How to remove a link from content in php?

Viewed 55026

How can i remove the link and remain with the text?

text text text. <br><a href='http://www.example.com' target='_blank' title='title' style='text-decoration:none;'>name</a>

like this:

text text text. <br>

i still have a problem.....

$text = file_get_contents('http://www.example.com/file.php?id=name');
echo preg_replace('#<a.*?>.*?</a>#i', '', $text)

in that url was that text(with the link) ...

this code doesn't work...

what's wrong?

Can someone help me?

9 Answers

Try this one. Very simple!

$content = "text text text. <br><a href='http://www.example.com' target='_blank' title='title' style='text-decoration:none;'>name</a>";
echo preg_replace("/<a[^>]+\>[a-z]+/i", "", $content);

Output:

text text text. <br>

Try:

$string = preg_replace( '@<(a)[^>]*?>.*?</\\1>@si', '', $string );

Note: this code remove link with text.

A version from the above compiled notes:

$withoutlink = preg_replace('/<a.*>(.*)<\/a>/isU','$1',$String);
Related