PHP - Trim string containing <a href=
I need a way of taking a string that contains <a href="url">something</a>
and remove开发者_开发问答 it completely from the string.
So,
$text = 'Something string <a href="url">something</a> some more string';
Any ideas?
strip_tags()
:
Removing an anchor is as simple as using this function
echo strip_tags($text)
In case you would want to allow the tag, just add it as a parameter.
str_replace()
:
$text = str_replace('<a href="url">something</a>', '', $text);
And, if there's a little more variables, preg_replace()
:
$text = preg_replace('/<a href="[^"]+">.+?</a>/i', '', $text);
However, there are several risks and shortcomings when combining HTML and regex.
I don't understand you question perfectly, but you can use these PHP functions to replace the string:
str_replace
str_replace();
or
preg_replace
preg_replace();
精彩评论