[英]Remove contents of href attribute with with RegEx
例如,我有以下HTML代码段:
<a href="/sites/all/themes/code.php">some text</a>
问题是-如何使用preg_replace()
从href
剪切文本/sites/all/themes/code.php
; 我可以使用哪种模式?
我强烈建议您不要使用正则表达式来解析任何SGML派生类。
对于HTML,请使用一些DOM解析器。 对于PHP,特别是DOMDocument 。
图案:
(<a .*?href=")([^"]*)
替换:$ 1
您不必做“替换”
(?<=<a href=")[^"]*(?=">)
直接为您带来您想要的东西。
用grep测试:
kent$ echo '<a href="/sites/all/themes/code.php">some text</a>'|grep -oP '(?<=<a href=")[^"]*(?=">)'
/sites/all/themes/code.php
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.