Regex or something else for links?

PHP programming forum. Ask questions or help people concerning PHP code. Don't understand a function? Need help implementing a class? Don't understand a class? Here is where to ask. Remember to do your homework!

Moderator: General Moderators

Post Reply
InternetX
Forum Newbie
Posts: 22
Joined: Tue Jan 21, 2003 1:57 pm
Location: Germany
Contact:

Regex or something else for links?

Post by InternetX »

Hi!

Have anyone a regex or something else to make links in a text automatically
to a http link? As example an user write:

http://www.domain.com

Then the php script make a link with this:

<a href="www.domain.com">www.domain.com</a>

Or is this not possible?


Edit:
This function is in this board software, too.
How works it?
User avatar
redhair
Forum Contributor
Posts: 300
Joined: Fri May 30, 2003 4:36 pm
Location: 53.23N-6.57E
Contact:

Re: Regex or something else for links?

Post by redhair »

InternetX wrote:
http://www.domain.com
intresting....a link.. hey did you do that? You just typed http://www.domain.com ...and it turned into a link :?:

go read this:
http://www.php.net/manual/en/function.preg-match.php
About a function that could check for 'www.' in your variable.
read also:
http://www.php.net/manual/en/function.preg-replace.php
How you can manipulate your variable...(make a link out of it)
InternetX
Forum Newbie
Posts: 22
Joined: Tue Jan 21, 2003 1:57 pm
Location: Germany
Contact:

Post by InternetX »

I will look... thanks...
User avatar
patrikG
DevNet Master
Posts: 4235
Joined: Thu Aug 15, 2002 5:53 am
Location: Sussex, UK

Post by patrikG »

A nice bit of googling yielded a nice little tutorial which I think is what you are looking for.

Before you read on: read the tutorial above :P


Ok. Great. Now that you've done that:

Code: Select all

<?php
$message='Bobs is neat http://www.bob.com as heck, the good Bob http://www.bob.com Bobs Bob drinks Guinness.';
echo "<xmp>$message</xmp>";
$message=preg_replace('/(www\.\b)+/i','http://\1',$message);

echo "<xmp>$message</xmp>";
?>
Is what you're really looking for. So, how about you buy me a pint now? :P
InternetX
Forum Newbie
Posts: 22
Joined: Tue Jan 21, 2003 1:57 pm
Location: Germany
Contact:

Post by InternetX »

Thanks *s*
Post Reply