Forum Moderators: coopster

Message Too Old, No Replies

Removing relative urls

How to remove reative links from text

         

adnanp78

9:06 am on Apr 26, 2007 (gmt 0)

10+ Year Member



How to remove reative links from text? This topic isapsulute unposible to find on internet. In fact, i neeed php function to remove all relative links from text and keep apsulute. Is someone so great in regexp? Tnx

adb64

9:31 am on Apr 26, 2007 (gmt 0)

10+ Year Member



Try the following:

function Replace($matches)
{
return strpos [php.net]($matches[0],"http://") === FALSE? "" : $matches[0];
}
$Pattern = "#<a.*>.*</a>#iU";
$NewText = preg_replace_callback [php.net]($Pattern,"Replace",$Text);

If your text has more than one line change $Pattern to

$Pattern = "#<a.*>.*</a>#iUms";

Regards,
Arjan

[edited by: adb64 at 10:00 am (utc) on April 26, 2007]

mcibor

9:37 am on Apr 26, 2007 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



I think what you could do is use two regexes:

1. find all links and get the href value

next check if it has http:// if not, then delete.

Hope this get's you on right track.

PS. There is an option to exclude sth in regex, but I never got it to work.

Regards
Michal

adnanp78

11:54 am on Apr 26, 2007 (gmt 0)

10+ Year Member



BUT!
We want to keep text in link, also just to remove all a href tags there are relative. Your code work perfect but it remove text too and erticle look terible after.
Thx Arjan

adb64

12:11 pm on Apr 26, 2007 (gmt 0)

10+ Year Member



Hi,

if you want to keep the text change code to this:


function Replace($matches)
{
return strpos($matches[0],"http://") === FALSE? $matches[1] : $matches[0];
}
$Pattern = "#<a.*>(.*)</a>#iU";
$NewText = preg_replace_callback($Pattern,"Replace",$Text);

Take care you don't forget the parenthesis in $Pattern.
Did not test, but I guess it should work now.

adnanp78

1:50 pm on Apr 26, 2007 (gmt 0)

10+ Year Member



great, tnx