我简单的匹配abba 这样的4个连续的数字,里面使用了子表达式和反向引用,结果没有捕获到,真不知道为什么,我哪里写错了?
<?phpheader("Content-type:text/html;charset=utf-8");$str="fdsafsdafsgfdag1223fa1551sdfassd3265";$mypreg="/(\d)(\d)\2\1/";//匹配abba式的数字,即上面的1551$mycon="这是数字";$str=preg_replace($mypreg,$mycon,$str);echo $str."<br/>";
可以呀
$str="fdsafsdafsgfdag1223fa1551sdfassd3265";$mycon="这是数字";$str=preg_replace('/(\d)(\d)\2\1/',$mycon,$str);echo $str."<br/>";fdsafsdafsgfdag1223fa这是数字sdfassd3265076402276aae5dbec7f672f8f4e5cc81
嗯,是双引号的问题
在双引号中字符的转义符生效了
$a = "/(\d)(\d)\2\1/";$b = '/(\d)(\d)\2\1/';var_dump($a == $b);var_dump($a, $b);bool(false) string(12) "/(\d)(\d)/" string(14) "/(\d)(\d)\2\1/"
$a = "/(\d)(\d)\\2\\1/";$b = '/(\d)(\d)\2\1/';var_dump($a == $b);var_dump($a, $b);bool(true) string(14) "/(\d)(\d)\2\1/" string(14) "/(\d)(\d)\2\1/"
我简单的匹配abba 这样的4个连续的数字,里面使用了子表达式和反向引用,结果没有捕获到,真不知道为什么,我哪里写错了?
<?phpheader("Content-type:text/html;charset=utf-8");$str="fdsafsdafsgfdag1223fa1551sdfassd3265";$mypreg="/(\d)(\d)\2\1/";//匹配abba式的数字,即上面的1551$mycon="这是数字";$str=preg_replace($mypreg,$mycon,$str);echo $str."<br/>";
$str="fasdgasgdfgfduuuuuu1221uuuuufasdfsdfsdaf4444";$mypreg='/(\d)\1{3}/i';$mycon="这是数字";echo $str."<br/>"; $str=preg_replace($mypreg,$mycon,$str);echo $str."<br/>";