views:

48

answers:

4

Hello everyone,

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?

A: 

strip_tags() will strip HTML tags.

Alan Haggai Alavi
A: 

I suggest you to keep the text in link.

strip_tags($text, '<br>');

or the hard way:

preg_replace('#<a.*?>(.*?)</a>#i', '\1', $text)

If you don't need to keep text in the link

preg_replace('#<a.*?>.*?</a>#i', '', $text)
how
He didn't indicate he wanted name to remain
methodin
thank you verry much
Adrian
preg_replace('#<a.*?>.*?</a>#i', '', $text) helped me
Adrian
@methodin: Thanks, fixed
how
A: 

Try:

preg_replace('/<a.*?<\/a>/','',"test test testa<br> <a href='http://www.example.com' target='_blank' title='title' style='text-decoration:none;'>name</a>");
methodin
+2  A: 

While strip_tags() is capable of basic string sanitization, it's not fool-proof. If the data you need to filter is coming in from a user, and especially if it will be displayed back to other users, you might want to look into a more comprehensive HTML sanitizer, like HTML Purifier. These types of libraries can save you from a lot of headache up the road.

strip_tags() and various regex methods can't and won't stop a user who really wants to inject something.

Ryan Chouinard
one up vote for HTML purifier :)
coolkid