开发者

PHP - Trim string containing <a href=

开发者 https://www.devze.com 2023-03-20 17:16 出处:网络
I need a way of taking a string that contains <a href=\"url\">something</a> and remove开发者_开发问答 it completely from the string.

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();
0

精彩评论

暂无评论...
验证码 换一张
取 消

关注公众号