I am wanting to find all links in a string of HTML that do not have a target
attribute so that it can be added.
Here is some code that detects the attributes... I could try search the output to find if there is a target but is there a simpler way I can do this to detect if it has a target attribute or not?
$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);
Output:
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
)
)