CLiown
CLiown

Reputation: 13853

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?

Upvotes: 0

Views: 379

Answers (3)

Pathik Gandhi
Pathik Gandhi

Reputation: 1344

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

Upvotes: 0

Antonio Laguna
Antonio Laguna

Reputation: 9300

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.

Upvotes: 3

jensgram
jensgram

Reputation: 31518

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.

Upvotes: 1

Related Questions