Forum Moderators: coopster

Message Too Old, No Replies

Replace non linking text with Active Link

         

ukgimp

1:31 pm on Apr 26, 2004 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



Hello

I am trying to replace any occurances of a non active bit of a string and making at active.

eg

"the link to www.webmasterworld.com was.."

to

the link to <a href="http://www.webmasterworld.com/forum/3">www.webmasterworld.com/forum3</a> was.."

Are there any functions for the url extraction in this case or do I need to do some regex to strip it.

$string = "the link to www.webmasterworld.com was..";
$pattern = "some pattern for any standard url"
$replacement = "<a href="http:\/\/www.webmasterworld\.com\">www\.webmasterworld\.com</a>";
preg_replace($pattern, $replacement, $string);

I cant see how to get the back reference in there as you would do in a URL rewrite.

Am I even in the right area :)

cheers

coopster

2:10 pm on Apr 26, 2004 (gmt 0)

WebmasterWorld Administrator 10+ Year Member



I think you are on the right track. In order to capture the subpattern and use it in a back reference, you simply surround the pattern with parentheses. Then use the back reference in your replacement. The preferred form of back references is the $n format.
$string = "the link to www.webmasterworld.com was.."; 
$pattern = "/(www.*com)/Uis";
$replacement = "<a href=\"http://$1\">$1</a>";
$string = preg_replace($pattern, $replacement, $string);

The regular expression for $pattern is where you will be spending some time in order to match the possibilities.

ukgimp

2:16 pm on Apr 26, 2004 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



Thats a great healp Coopster

Can I ask what the Uis after the pattern?

Cheers

ergophobe

2:53 pm on Apr 26, 2004 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member Top Contributors Of The Month



i - case insensitive

s - match all characters including newlines with the dot metacharacter so that if the URL is split over two or more lines, the regex still works.

U - ungreedy. This means it stops at the first "com" which means that I would probably choose to search for

$pattern = "/(www.*\.com)/Uis";

so it stops at the first ".com" - note this will still be a problem with domains like "www.commercial.com". You may wish to stop at the first space if you *know* the URI will be followed by a space, as in

$pattern = "/(www[^\s]*)/Uis";

Tom

ukgimp

3:48 pm on Apr 26, 2004 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



Thanks for that. Geez I am having some right trouble with this one I seem to get 90% there.

I have hacked a bit of code to almost do it. I was wondering if I could get a pointer. I think my regex is to blame.

I get the following error

Warning: Delimiter must not be alphanumeric or backslash in

Cheers

//######### Code

$string = "www.test.com/test/test.htm www.test2.co.uk";
$pattern = "/www/";
$replacement = "http://www";
$string = preg_replace($pattern, $replacement, $string);

print "$string<hr>";

$urls = '(http¦file¦ftp)';
$ltrs = '\w';
$gunk = '/#~:.?+=&%@!\-';
$punc = '.:?\-';
$any = "$ltrs$gunk$punc";
preg_match_all("{
\b
$urls :
[$any] +?

(?=
[$punc] *
[^$any]
¦
$
)
}x", $string, $matches);
foreach ($matches[0] as $u) {
echo "<a href='$u'>$u</a><br>\n";

//Do a preg_replace here for each value of $u in the string
// However it is here where it goes mighty wrong
$pattern = "$u";
$replacement = "<a href=\"$u\">$u</a>";
$string = preg_replace($pattern, $replacement, $string);

}
print "<b>$string</b>";

ergophobe

4:20 pm on Apr 26, 2004 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member Top Contributors Of The Month



Forget what follows:

Since PHP 4.0.4, you can also use Perl-style (), {}, [], and <> matching delimiters.

This is wrong:

{} are min/max quantifiers. You need a delimiter for your pattern, so it should be

"/pattern/"

ergophobe

4:51 pm on Apr 26, 2004 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member Top Contributors Of The Month



You are not escaping your final pattern. Try using preg_quote.

foreach ($matches[0] as $u) {
echo "<a href='$u'>$u</a><br>\n";

//Do a preg_replace here for each value of $u in the string
// However it is here where it goes mighty wrong
$pattern = "{" . preg_quote($u) . "}";

ukgimp

8:04 am on Apr 27, 2004 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



ergophobe

Thanks for your help. Patterns mangle my mind :)

ergophobe

2:48 pm on Apr 29, 2004 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member Top Contributors Of The Month



Me too. I respond to regex posts mostly to make myself work through the regex for practice, not because I'm good at them! It's like homework assignments that actually help someone.

I probably write 25 regex per day for my regular work (research that involves grepping through text files for variant spellings from 16-th century documents), but most of them are dead easy, so it's fun to get a little beyond that. When it goes way beyond that, I have to call Coopster and Timster... (those guys aren't related are they?).

Tom