很抱歉问这个简单的问题,但我似乎找不到任何答案。
如何检查字符串中是否有数字?
我试过:
$string = "this is a simple string with a number 2432344";
if (preg_match('/[^0-9]/', "$string")) {
echo "yes a number";
} else {
echo "no number";
}
似乎不起作用...
如果要
查找数字,请不要在正则表达式中否定带有^
的字符集。这意味着"匹配除数字以外的任何内容"。
$string = "this is a simple string with a number 2432344";
if (preg_match('/[0-9]/', "$string")) {
echo "yes a number";
} else {
echo "no number";
}
此外,您可以只使用 'd
而不是 [0-9]
,$string
而不是 "$string"
。
^
只会否定正则表达式中的内容。你不需要使用它。