这里移除标记是我的字符串:正则表达式围绕一个特定的字符串
$str="<p>Some <a href="#">link</a> with <a href="http://whatever.html?bla">LINK2</a> and <a href="http://whatever.html?bla" target="_blank">LINK3</a> and</p> more html"
我想链接1和连接2使用PHP去删除:
"<p>Some <a href="#">link</a> with and and</p> more html"
这是我想接近我需要的:
$find = array("<a(.*)LINK1(.*)</a>", "<a(.*)LINK2(.*)</a>");
$replace = array("", "");
$result=preg_replace("$find","$replace",$str);
这是行不通的。我已经搜索了几天,并尝试了很多其他的选择,但从来没有设法让这个按预期工作。另外,我并不介意LINK1和2在移除a标签后立即出现。
请不要使用正则表达式解析HTML,因为它会[驱动你į̷̷͚̤̤̖̦͍͗̒̈̅̄n̨͖͓̹͍͎͔͈̝͐ͪ͛̄͛ṣ̷̵̞̦ͤ̅̉̋ͪ͑͛ͥ͜a̷̘͖̮͔͎͛̇̏̒͆̆͘n͇͔̤̼͙̩͖̭ͤ͋̉͌͟eͥ͒͆ͧͨ̽͞҉̹͍̳̻͢](http://stackoverflow.com/questions/1732348/regex-match-open-tags-except-xhtml-self-contained-tags/1732454 #1732454)。改为使用[HTML解析器](http://stackoverflow.com/questions/292926/robust-mature-html-parser-for-php)。 – 2012-07-28 22:34:26
**不要使用正则表达式来解析HTML。使用合适的HTML解析模块**您无法可靠地使用正则表达式解析HTML,并且您将面临悲伤和挫折。只要HTML从你的期望改变,你的代码就会被破坏。请参阅http://htmlparsing.com/php或[this SO thread](http://stackoverflow.com/questions/3577641/how-do-you-parse-and-process-html-xml-in-php)如何使用已经编写,测试和调试的PHP模块正确解析HTML的示例。 – 2013-08-29 19:49:50