14

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?

Adrian
  • 141
  • 1
  • 1
  • 4

9 Answers9

43

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)
luchaninov
  • 6,792
  • 6
  • 60
  • 75
4

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
  • 2,076
  • 16
  • 11
3

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;}
RRikesh
  • 14,112
  • 5
  • 49
  • 70
Vu Anh
  • 209
  • 2
  • 7
3

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
  • 6,717
  • 1
  • 25
  • 27
1

A version from the above compiled notes:

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

strip_tags() will strip HTML tags.

Alan Haggai Alavi
  • 72,802
  • 19
  • 102
  • 127
0

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>
icaksama
  • 712
  • 10
  • 15
0

Try:

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

Note: this code remove link with text.

Mahdi Akrami
  • 466
  • 1
  • 4
  • 12
-1

One more short solution without regexps:

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;
    }
}
?>
dmikam
  • 992
  • 1
  • 18
  • 27
  • 1
    Regex is much simpler, saver and by far more efficient than a while loop with string manipulation like this. Just imagine the HTML code contains ``, then this code will possibly lose a lot of text... – Philipp May 13 '17 at 20:06
  • 1
    agree with you. – dmikam May 16 '17 at 23:58