我正在尝试从 PHP 中的变量中删除除数字之外的所有内容。我尝试使用正则表达式,很快谷歌就会出现各种各样的人告诉我也使用正则表达式,但似乎没有正则表达式起作用。我正在使用preg_replace('/\D/', '', $amount);. 我尝试了各种不同的正则表达式,最值得注意的是/\D/, /[\D]/, /^\d/, /[^\d]/, and /[^0-9]/,但它们都不起作用。
编辑:我找到了为什么它不起作用!我的印象是preg_replace('/\D/', '', $amount);会替换$amount,但我现在看到我必须$new_amount = preg_replace('/\D/', '', $amount);使用 $new_amount。愚蠢,我知道。不管怎么说,还是要谢谢你!