我想在没有target
属性的 HTML 字符串中找到所有链接,以便可以添加它。
这是一些检测属性的代码...我可以尝试搜索输出以查找是否有目标,但是有没有更简单的方法可以检测它是否具有目标属性?
$content = '<p>This is some <a href="http://www.google.com">sample text</a> with
<a href="htttp://bing.com" target="_blank" class="test">links</a>.</p>';
preg_match_all('/<a([^>]*)href="([^"]*)"([^>]*)>([^<]*)<\/a>/', $content, $matches);
print_r($matches);
输出:
Array
(
[0] => Array
(
[0] => <a href="http://www.google.com">sample text</a>
[1] => <a href="htttp://bing.com" target="_blank" class="test">links</a>
)
[1] => Array
(
[0] =>
[1] =>
)
[2] => Array
(
[0] => http://www.google.com
[1] => htttp://bing.com
)
[3] => Array
(
[0] =>
[1] => target="_blank" class="test"
)
[4] => Array
(
[0] => sample text
[1] => links
)
)