Warning: file_get_contents(/data/phpspider/zhask/data//catemap/0/search/2.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Php 查找字符串中存在的数值_Php_Search_Find - Fatal编程技术网

Php 查找字符串中存在的数值

Php 查找字符串中存在的数值,php,search,find,Php,Search,Find,如何查找字符串中存在的数字 在上面的例子中,st1、st2只需使用preg_match $st1='sdfsffgfgfdgf4324csadsaffas34df' $st2='saddsd56856785 dfdffffv' $st3='fdasfdsfdsfdsfdsfdffd' 使用 使用正则表达式 $result = preg_match('/[0-9]/', $st1); 使用。此函数扫描字符串以查找与模式的匹配项,然后用替换替换匹配的文本 preg_match('/[0-9]/'

如何查找字符串中存在的数字


在上面的例子中,st1、st2只需使用
preg_match

$st1='sdfsffgfgfdgf4324csadsaffas34df'
$st2='saddsd56856785 dfdffffv'
$st3='fdasfdsfdsfdsfdsfdffd'
使用


使用正则表达式

$result = preg_match('/[0-9]/', $st1);
使用。此函数扫描字符串以查找与模式的匹配项,然后用替换替换匹配的文本

preg_match('/[0-9]/', $st1, $matches);
return !empty($matches); // returns true if there is a number in $st1


因此,基本上任何包含至少一个数字的字符串都符合描述。你试过确定吗?问题不应该因为你认为答案简单就被否决。并不是每个人都有相同的技能水平。不过,ereg功能现在已经贬值了。
preg_match('/[0-9]/', $st1, $matches);
return !empty($matches); // returns true if there is a number in $st1
<?php
$ms="String321";
$string = ereg_replace("[^0-9]", "",$ms);
echo $string;
?>
function containsNumeric($str) {
    preg_match("/\\d/", $str, $matches);
    return !empty($matches);
}

echo containsNumeric("ab2c"); // true