Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to remove a link from content in php?

Tags:

php

hyperlink

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?

like image 205
Adrian Avatar asked Sep 30 '10 13:09

Adrian


People also ask

How do I remove a hyperlink link?

To remove a hyperlink but keep the text, right-click the hyperlink and click Remove Hyperlink. To remove the hyperlink completely, select it and then press Delete.

How can I remove part of a string in PHP?

The substr() and strpos() function is used to remove portion of string after certain character.

How do you unlink a link in HTML?

Just select the text where you want to remove the links, click the Insert/edit link, then leaving all fields empty click Ok.


2 Answers

this is 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;}
like image 78
Vu Anh Avatar answered Oct 06 '22 00:10

Vu Anh


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)
like image 35
luchaninov Avatar answered Oct 06 '22 01:10

luchaninov