Maison  >  Article  >  développement back-end  >  网上一段简略的preg_match替换 不明

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

WBOY
WBOYoriginal
2016-06-13 12:36:38992parcourir

网上一段简单的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?正则
Déclaration:
Le contenu de cet article est volontairement contribué par les internautes et les droits d'auteur appartiennent à l'auteur original. Ce site n'assume aucune responsabilité légale correspondante. Si vous trouvez un contenu suspecté de plagiat ou de contrefaçon, veuillez contacter admin@php.cn