I use preg_replace
function that has to clear all the rel
and target
attributes from the $body
and to replace them with other values. Here it is:
$patterns = array();
$patterns[] = '/<a(.*) rel="([^"]*)"(.*)>/';
$patterns[] = '/<a(.*) target="([^"]*)"(.*)>/';
$patterns[] = '/<a(.*)>/';
$replacements = array();
$replacements[] = '<a$1$3>';
$replacements[] = '<a$1$3>';
$replacements[] = '<a rel="nofollow" target="_blank"$1>';
$body = preg_replace($patterns,$replacements,$body);
The problem is that it does not match single and no-quotes.
Also if there is any better approach for clearing the rel
and target
attributes from the links and setting them with others, please advise.
Thanks
EDIT: $body
:
$body = '<a href="TEST">Link1</a>
<a href="TEST" rel=\'lqlqlq\'>Link2</a>
<a href="TEST" target="_blank" rel="lqlqlq">Link3</a>
<a href="TEST" target=_blank rel=lqlqlq>Link4</a>';
And also every other option for working links due to the text is user defined and probably some users are going to cheat. My goal - all of the links in $body
to be with defined rel
and target
attributes no matter what the user has entered.