Go Back   CodingForums.com > :: Server side development > PHP

Before you post, read our: Rules & Posting Guidelines

Reply
 
Thread Tools Rate Thread
Enjoy an ad free experience by logging in. Not a member yet? Register.
Old 08-14-2008, 11:37 AM   PM User | #1
the-dream
Regular Coder

 
the-dream's Avatar
 
Join Date: Mar 2007
Location: Northamptonshire, UK
Posts: 477
Thanks: 8
Thanked 4 Times in 4 Posts
the-dream can only hope to improve
URL parsed from Text into HREF?

Hi,

I have some text that is being grabbed from a database and i want to parse out all the urls from being like:

This is my site www.example.com its cool!

to...

This is my site <a href="http://www.example.com/">www.example.com</a> its cool!

Does anyone know of any classes or premade scripts that will do this, I really don't want to write it myself!

Thanks,
~ Christian
__________________
Branchr Advertising Network
the-dream is offline   Reply With Quote
Old 08-14-2008, 01:46 PM   PM User | #2
mlseim
Master Coder

 
mlseim's Avatar
 
Join Date: Jun 2003
Location: Cottage Grove, Minnesota
Posts: 9,045
Thanks: 8
Thanked 1,029 Times in 1,020 Posts
mlseim has a spectacular aura aboutmlseim has a spectacular aura aboutmlseim has a spectacular aura about
Below is a function from phpbb.com ...

I put in a string with 3 different kinds of links to test it.

PHP Code:
<?php

// Here is the test string with three types of links ...

$string "
Lorem ipsum dolor sit amet, consectetuer adipiscing elit, sed diam
nonummy nibh euismod tincidunt ut laoreet dolore magna aliquam
erat volutpat. Ut wisi enim ad minim veniam, quis nostrud exerci
tation ullamcorper suscipit www.google.com ut aliquip ex ea commodo
Duis autem vel eum iriure dolor in hendrerit in vulputate velit esse
molestie consequat, vel illum dolore eu feugiat nulla facilisis at vero
eros et accumsan et iusto odio dignissim qui blandit praesent luptatum
zzril delenit augue duis dolore te feugait nulla facilisi.
<br /><br />
Nam liber tempor cum soluta nobis eleifend option congue nihil imperdiet
doming id quod mazim placerat facer possim assum. Email me: john@aol.com
<br /><br />
Lorem ipsum dolor sit amet, consectetuer adipiscing elit, sed diam
nonummy nibh euismod http://www.yahoo.com ut laoreet dolore magna aliquam erat
volutpat. Ut wisi enim ad minim veniam, quis nostrud exerci tation ullam
corper suscipit lobortis nisl ut aliquip ex ea commodo consequat.
<br /><br />
"
;

echo 
clickable_link($string);


function 
clickable_link($text)
{
# this functions deserves credit to the fine folks at phpbb.com

$text preg_replace('#(script|about|applet|activex|chrome):#is'"\\1:"$text);

// pad it with a space so we can match things at the start of the 1st line.
$ret ' ' $text;

// matches an "xxxx://yyyy" URL at the start of a line, or after a space.
// xxxx can only be alpha characters.
// yyyy is anything up to the first space, newline, comma, double quote or <
$ret preg_replace("#(^|[\n ])([\w]+?://[\w\#$%&~/.\-;:=,?@\[\]+]*)#is""\\1<a href=\"\\2\" target=\"_blank\">\\2</a>"$ret);

// matches a "www|ftp.xxxx.yyyy[/zzzz]" kinda lazy URL thing
// Must contain at least 2 dots. xxxx contains either alphanum, or "-"
// zzzz is optional.. will contain everything up to the first space, newline,
// comma, double quote or <.
$ret preg_replace("#(^|[\n ])((www|ftp)\.[\w\#$%&~/.\-;:=,?@\[\]+]*)#is""\\1<a href=\"http://\\2\" target=\"_blank\">\\2</a>"$ret);

// matches an email@domain type address at the start of a line, or after a space.
// Note: Only the followed chars are valid; alphanums, "-", "_" and or ".".
$ret preg_replace("#(^|[\n ])([a-z0-9&\-_.]+?)@([\w\-]+\.([\w\-\.]+\.)*[\w]+)#i""\\1<a href=\"mailto:\\2@\\3\">\\2@\\3</a>"$ret);

// Remove our padding..
$ret substr($ret1);
return 
$ret;
}

?>
mlseim is offline   Reply With Quote
Old 08-14-2008, 02:36 PM   PM User | #3
the-dream
Regular Coder

 
the-dream's Avatar
 
Join Date: Mar 2007
Location: Northamptonshire, UK
Posts: 477
Thanks: 8
Thanked 4 Times in 4 Posts
the-dream can only hope to improve
That is really GREAT! Thank you so much! Did you write it yourself?
__________________
Branchr Advertising Network
the-dream is offline   Reply With Quote
Old 08-14-2008, 06:37 PM   PM User | #4
mlseim
Master Coder

 
mlseim's Avatar
 
Join Date: Jun 2003
Location: Cottage Grove, Minnesota
Posts: 9,045
Thanks: 8
Thanked 1,029 Times in 1,020 Posts
mlseim has a spectacular aura aboutmlseim has a spectacular aura aboutmlseim has a spectacular aura about
No,

I found it on the internet, and within the function is this line ...
# this functions deserves credit to the fine folks at phpbb.com

But I also have a use for it, so I remembered that I had it on my PC.
mlseim is offline   Reply With Quote
Reply

Bookmarks

Jump To Top of Thread


Thread Tools
Rate This Thread
Rate This Thread:

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off

Forum Jump


All times are GMT +1. The time now is 12:17 PM.


Advertisement
Log in to turn off these ads.