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

WBOY
發布: 2016-06-13 12:36:38
原創
989 人瀏覽過

网上一段简单的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?正则
相關標籤:
來源:php.cn
本網站聲明
本文內容由網友自願投稿,版權歸原作者所有。本站不承擔相應的法律責任。如發現涉嫌抄襲或侵權的內容,請聯絡admin@php.cn
熱門教學
更多>
最新下載
更多>
網站特效
網站源碼
網站素材
前端模板
關於我們 免責聲明 Sitemap
PHP中文網:公益線上PHP培訓,幫助PHP學習者快速成長!