<?
$src='<center>
<a href="/ref-1-k-12-type-GuidedPractice" target="_self">Guided Practice</a> |
<a href="/ref-1-k-12-type-VideoClip" target="_self">Video Clip</a> |
<a href="/ref-1-k-12-type-Exercise" target="_self">Exercise</a> |
<a href="/ref-1-k-12-type-MathExperiment" target="_self">Experiment</a> |
<a href="/ref-1-k-12-type-MathGame" target="_self">Game</a> |
<a href="/ref-1-k-12-type-GraphingTool" target="_self">Graphing Tool</a> |
<a href="/ref-1-k-12-type-Modeling" target="_self">Modeling</a>|
<a href="/ref-1-k-12-type-Demo" target="_self">Demo</a> |
<a href="/ref-1-k-12-type-VirtualReality" target="_self">Virtual Reality Media</a>
</center>';
$src=preg_replace('/\\|[\\s\\S]*?\\<a href[\\s\\S]*?\\>Exercise\\<\\/a\\>/is','',$src);
echo($src);
?>
输出为<center>
<a href="/ref-1-k-12-type-GuidedPractice" target="_self">Guided Practice</a> |
<a href="/ref-1-k-12-type-MathExperiment" target="_self">Experiment</a> |
<a href="/ref-1-k-12-type-MathGame" target="_self">Game</a> |
<a href="/ref-1-k-12-type-GraphingTool" target="_self">Graphing Tool</a> |
<a href="/ref-1-k-12-type-Modeling" target="_self">Modeling</a>|
<a href="/ref-1-k-12-type-Demo" target="_self">Demo</a> |
<a href="/ref-1-k-12-type-VirtualReality" target="_self">Virtual Reality Media</a>
</center>这一行也被去掉了
<a href="/ref-1-k-12-type-VideoClip" target="_self">Video Clip</a> |
echo $src;
$src=preg_replace('#\|[^<]+<a href[^>]+>Exercise</a>#is','',$src);要过滤Exercise所在标签的话,这么写,你的斜杠确实写的太多了
$src='<center>
<a href="/ref-1-k-12-type-Exercise" target="_self">Exercise</a> |
<a href="/ref-1-k-12-type-GuidedPractice" target="_self">Guided Practice</a> |
<a href="/ref-1-k-12-type-VideoClip" target="_self">Video Clip</a> |
<a href="/ref-1-k-12-type-Exercise" target="_self">Exercise</a> |
<a href="/ref-1-k-12-type-MathExperiment" target="_self">Experiment</a> |
<a href="/ref-1-k-12-type-MathGame" target="_self">Game</a> |
<a href="/ref-1-k-12-type-GraphingTool" target="_self">Graphing Tool</a> |
<a href="/ref-1-k-12-type-Modeling" target="_self">Modeling</a>|
<a href="/ref-1-k-12-type-Demo" target="_self">Demo</a> |
<a href="/ref-1-k-12-type-VirtualReality" target="_self">Virtual Reality Media</a>
</center>';
echo($src."\n\n");
$src=preg_replace('#([|])?[^|<>]+<a href[^>]+>Exercise</a>(?(1)|[^<>]+)#is','',$src);
echo($src);另外还有这种情况,如果第一个链接是Exercise的时候,前面并没有 | 符号,则是要抹去Exercise链接和“后面的 | 符号”。用条件匹配。
$src='<center>
<a href="/ref-1-k-12-type-GuidedPractice" target="_self">Guided Practice</a> |
<a href="/ref-1-k-12-type-VideoClip" target="_self">Video Clip</a> |
<a href="/ref-1-k-12-type-Exercise" target="_self">Exercise</a> |
<a href="/ref-1-k-12-type-MathExperiment" target="_self">Experiment</a> |
<a href="/ref-1-k-12-type-MathGame" target="_self">Game</a> |
<a href="/ref-1-k-12-type-GraphingTool" target="_self">Graphing Tool</a> |
<a href="/ref-1-k-12-type-Modeling" target="_self">Modeling</a>|
<a href="/ref-1-k-12-type-Demo" target="_self">Demo</a> |
<a href="/ref-1-k-12-type-VirtualReality" target="_self">Virtual Reality Media</a>
</center>';
$src=preg_replace('/\|[\s\S]*?\<a href[\s\S]*?\>(Exercise)\<\/a\>/is','',$src);
echo($src);
?>
代码中红色和绿色的都没了,而我只要去掉绿色的
非贪婪模式是指匹配到下一个条件生效的最短匹配,这里应该和非贪婪没什么关系,按你写的正则匹配就是最短的了
你出错是因为\S使得第二行中的href早于第三行的href进行匹配,尽量少使用\S,尽量精确匹配到当前所在行
$src=preg_replace('/\|[\s\S]*?\<a href[\s\S]*?\>(Exercise)\<\/a\>/is','',$src);
我的正则,先匹配的|,然后直接匹配到 Exercise </a>
而我开始以为,有非贪婪的话就可以以 Exercise </a>为基准,在前面匹配尽量少的内容, 而正则看来不是这样的机制