Reputation: 15339
probably it's something easy for most of you, but I've been strugling to replace all <a href="url">
anything </a>
from a string.
Can someone help me out with this and give me some pointers with documentations and examples? The php.net doesn't help much, struggling to understand.
From what I realize I need to replace:
'<a href=*' up until the '>' character is met.
Replacing the </a>
I can manage :)
Upvotes: 0
Views: 468
Reputation: 78994
Could be simpler, but given the requirements and not knowing of any variations in your string:
$new = preg_replace('/<a href=[^>]+>/', '', str_replace('</a>', '', $string));
Upvotes: 0
Reputation: 12571
What would be wrong with using the strip_tags function in PHP? Are you absolutely trying to use a regular expression (something I would advise against when working with HTML)?
The PHP documentation example does just what you want:
<?php
$text = '<p>Test paragraph.</p><!-- Comment --> <a href="#fragment">Other text</a>';
echo strip_tags($text);
echo "\n";
// Allow <p> and <a>
echo strip_tags($text, '<p><a>');
?>
Results:
Test paragraph. Other text
<p>Test paragraph.</p> <a href="#fragment">Other text</a>
Upvotes: 3