Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/php/277.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中字符串的前3个字符_Php_Regex - Fatal编程技术网

正则表达式匹配PHP中字符串的前3个字符

正则表达式匹配PHP中字符串的前3个字符,php,regex,Php,Regex,我正在寻找匹配字符串前3个字符的模式正则表达式。 例如,我有两个这样的数字: 011569875和041568956 如果我找到了前3个字符011,那么我将按照@Mark的建议打印011569875,您可以使用substr,如下所示: $string = substr($longstring,0,3); if($string == '011'){ echo $longstring; } 希望这有帮助。干杯您不需要正则表达式来测试它,为什么不简单地使用substr()?我如何使用subs

我正在寻找匹配字符串前3个字符的模式正则表达式。
例如,我有两个这样的数字:
011569875
041568956


如果我找到了前3个字符
011
,那么我将按照@Mark的建议打印
011569875
,您可以使用
substr
,如下所示:

$string = substr($longstring,0,3);
if($string == '011'){
   echo $longstring;
}

希望这有帮助。干杯

您不需要正则表达式来测试它,为什么不简单地使用
substr()
?我如何使用
substr()
来实现这一目的?
if(substr($MyString,0,3)=“011”){echo$MyString;}
echo substr($number,0,3)=“011”$编号:''假设您的值确实是strings,但这不是正则表达式解决方案。