How to remove link from content in php?

How to remove the link and stay 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 this url was this text (with link) ...

this code does not work ...

what's wrong?

Can someone help me?

+4
source share
6 answers

I suggest you keep the text in the link.

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

or the hard way:

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

If you do not need to save the text in the link

 preg_replace('#<a.*?>.*?</a>#i', '', $text) 
+18
source

Although strip_tags() is capable of basic line disinfection, it is not flawless. If the data you want to filter comes from the user, and especially if it is displayed back to other users, you might want to explore a more comprehensive HTML sanitizer, such as HTML Cleaner . These types of libraries can save you a lot of headaches along the way.

strip_tags() , and the various regex methods cannot and will not stop a user who really wants to enter something.

+4
source

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>"); 
+3
source

strip_tags() will split the HTML tags.

+1
source

these are my solutions:

 function removeLink($str){ $regex = '/<a (.*)<\/a>/isU'; preg_match_all($regex,$str,$result); foreach($result[0] as $rs) { $regex = '/<a (.*)>(.*)<\/a>/isU'; $text = preg_replace($regex,'$2',$rs); $str = str_replace($rs,$text,$str); } return $str;} 

dang tin rao vat

0
source

Another short solution without regular expressions:

 function remove_links($s){ while(TRUE){ @list($pre,$mid) = explode('<a',$s,2); @list($mid,$post) = explode('</a>',$mid,2); $s = $pre.$post; if (is_null($post))return $s; } } ?> 
-1
source

All Articles