Forum Moderators: coopster

Message Too Old, No Replies

Recognizing URLs in Text, and Parsing them into Hyperlinked HTML urls

         

kazisdaman3

10:59 am on Mar 2, 2007 (gmt 0)

10+ Year Member



Hi everyone, couldn't find the answer from searching.

PHP & MySQL:

How can I recognize a url, and turn it into a HTML hyperlinked url.

without, having like [url]http://www.example.com[/url]

I got the above to work, but can I get it to recongize without the [url][/url]and have it convert the url to a hyperlinked url?

Thanks for the help, I appreciate you guys help all the time, thank you!

Warmly,
Wesley

eelixduppy

12:00 pm on Mar 2, 2007 (gmt 0)



Try something like this.

$pattern = "/http:\/\/(www\.)?([^.]+\.[^.\s]+\.?[^.\s]*)/i";
$replace = "<a href='http://\\1\\2'>http://\\1\\2</a>";
$string = [url=http://us2.php.net/preg-replace]preg_replace[/url]($pattern,$replace,$string);
echo $string;

Where

$string
is the text. Just a couple notes about this pattern:
1) The URI MUST have "http://" before it
2) It works for URIs formatted in the following ways
>>>http://www.example.foo
>>>http://www.example.foo.foo #having two
>>>http://example.foo
>>>http://example.foo.foo #having two
>>>http://foo.exmaple.foo #pretty much the same as above

I know this is missing a possibility but I figured I'd get you on the right track ;)

Good luck!

kazisdaman3

12:19 pm on Mar 2, 2007 (gmt 0)

10+ Year Member



Wow,

! Thanks a ton again eelixduppy!

I owe you a christmas gift now!x3

Thank you so much!

-WES