>  기사  >  백엔드 개발  >  网上一段简略的preg_match替换 不明

网上一段简略的preg_match替换 不明

WBOY
WBOY원래의
2016-06-13 12:36:38990검색

网上一段简单的preg_match替换 不明

<br />
$string = "Is is the cost of of gasoline going up up"; <br />
	$pattern = "/\b([a-z]+) \\1\b/i"; <br />
	if(preg_match($pattern, $string,$arr)){<br />
		print_r($arr);<br />
		echo preg_replace($pattern, '$1', $string);<br />
	}<br />

输出
<br />
Array<br />
(<br />
    [0] => Is is<br />
    [1] => Is<br />
)<br />
Is the cost of gasoline going up<br />


我理解的preg_match($pattern,$repalce,$subject)是用pattern从subject匹配到各个分组,然后用replace规定的显示方式,重新输出
$pattern = "/\b([a-z]+) \\1\b/i" 匹配到的只有Is is 和Is
重新输出怎么会输出一整句话 Is the cost of gasoline going up
还有什么时候用$1 什么时候用\\1 获取子模式匹配的内容 在双引号和单引号里又有什么要求?

php?正则
성명:
본 글의 내용은 네티즌들의 자발적인 기여로 작성되었으며, 저작권은 원저작자에게 있습니다. 본 사이트는 이에 상응하는 법적 책임을 지지 않습니다. 표절이나 침해가 의심되는 콘텐츠를 발견한 경우 admin@php.cn으로 문의하세요.