Heim  >  Artikel  >  Backend-Entwicklung  >  网上一段简略的preg_match替换 不明

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

WBOY
WBOYOriginal
2016-06-13 12:36:38989Durchsuche

网上一段简单的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?正则
Stellungnahme:
Der Inhalt dieses Artikels wird freiwillig von Internetnutzern beigesteuert und das Urheberrecht liegt beim ursprünglichen Autor. Diese Website übernimmt keine entsprechende rechtliche Verantwortung. Wenn Sie Inhalte finden, bei denen der Verdacht eines Plagiats oder einer Rechtsverletzung besteht, wenden Sie sich bitte an admin@php.cn