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

WBOY
Release: 2016-06-13 12:36:38
Original
989 people have browsed it

网上一段简单的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 />
Copy after login

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


我理解的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?正则
Related labels:
source:php.cn
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template
About us Disclaimer Sitemap
php.cn:Public welfare online PHP training,Help PHP learners grow quickly!