我使用js的正则没有问题,然后同样的写成php正则,居然匹配不到,两者的正则不一样?
如,我匹配4个连续且全部相同的数字,同样的串,同样的规则,js可以匹配成功,php匹配不到,
js:
var str="fasdgasgdfgfduuuuuu1221uuuuufasdfsdfsdaf4444";var myreg=/(d){3}/gi;var mycon="数字";var str=str.replace(myreg,mycon);document.write(str);<?phpheader("Content-type:text/html;charset=utf-8");$str="fasdgasgdfgfduuuuuu1221uuuuufasdfsdfsdaf4444";$mypreg="/(d){3}/i";$mycon="这是数字";echo $str."<br/>"; $str=preg_replace($mypreg,$mycon,$str);echo $str."<br/>"; $str="fasdgasgdfgfduuuuuu1221uuuuufasdfsdfsdaf4444";
$mypreg="/(d{4})/i";
$mycon="this is number";
$str=preg_replace($mypreg,$mycon,$str);
echo $str."
";
//fasdgasgdfgfduuuuuuthis is numberuuuuufasdfsdfsdafthis is number
我觉得主要问题是php无法使用反向捕获的原因。
或者php的反向捕获不是 这样的
双引号中转义生效了!
$a = "/(d){3}/i";$b = '/(d){3}/i';var_dump($a == $b);var_dump($a, $b); 我使用js的正则没有问题,然后同样的写成php正则,居然匹配不到,两者的正则不一样?
如,我匹配4个连续且全部相同的数字,同样的串,同样的规则,js可以匹配成功,php匹配不到,
js:
var str="fasdgasgdfgfduuuuuu1221uuuuufasdfsdfsdaf4444";var myreg=/(d){3}/gi;var mycon="数字";var str=str.replace(myreg,mycon);document.write(str);<?phpheader("Content-type:text/html;charset=utf-8");$str="fasdgasgdfgfduuuuuu1221uuuuufasdfsdfsdaf4444";$mypreg="/(d){3}/i";$mycon="这是数字";echo $str."<br/>"; $str=preg_replace($mypreg,$mycon,$str);echo $str."<br/>";$str="fasdgasgdfgfduuuuuu1221uuuuufasdfsdfsdaf4444";$mypreg='/(d){3}/i';$mycon="这是数字";echo $str."<br/>"; $str=preg_replace($mypreg,$mycon,$str);echo $str."<br/>";建议无论是不是双引号和单引号之中,如果想用单个 字符,最好都是用 \来表示,这样是不会出现歧义的,尽管单引号仅检测 '转义
PHP怎么学习?PHP怎么入门?PHP在哪学?PHP怎么学才快?不用担心,这里为大家提供了PHP速学教程(入门到精通),有需要的小伙伴保存下载就能学习啦!
Copyright 2014-2025 https://www.php.cn/ All Rights Reserved | php.cn | 湘ICP备2023035733号