...

View Full Version : Parse a url and convert to link.



Mhtml
04-06-2003, 07:04 AM
Say I have my string
$mess = "visit www.codingforums.com"; , how can I convert that into a link?

Should I split up to variable into an array and search it and then go from there? (Can PHP do that?)

Spookster
04-06-2003, 07:40 AM
Is this for your shout box? Trying to automatically parse URL's and create hyperlinks out of them?

Yes you can parse the strings looking for patterns that might be a URL. RegEx would be your best bet. Not good enough with them to come up with a pattern for that though. Of course you could always download any message board software like this one and look at how they do it.

Mhtml
04-06-2003, 07:45 AM
Yup it's for my shout box.
PHP-Dev came with invision board so I'll have a look in the source.

I'd never had thought of that myself.http://m5interactive.com/emoticons/jest.gif Thanks spookster! :D

Mhtml
04-06-2003, 11:25 AM
Well after a lot of searching through both phpBB and Invision Board I've given up. lol Just couldn't find it.... I googled as well.. no such luck.

firepages
04-06-2003, 01:51 PM
I too am regex challenged .... luckily I reckon Mordred should turn up soon :)

but , and the reason I posted is .... use the manual luke..

if you look at preg_replace in the manual (download the CHM version) there are loads of examples , I grabbed this from there , its not perfect but anyway...

(the bold is to stop the forum stuffing up the slashes)



<?
$text="
`oh sweet yakdom` by da yak http://www.yaks.com ,
get the ripped CD @ ftp://yaks.net/cd?llamas=smelly , cool
";

$text = preg_replace(
"/(?:^|\b)((((http|https|ftp):\/\/)|(www\.))([\w\.]+)([,:%#&\/?=\w+\.-]+))(?:\b|$)/is",
"<a href=\"$1\" target=\"_blank\">$1</a>",
$text
);

echo $text;
?>


I have a version of the phpBB BBcode parser but its pretty `slack` at best (search for bbcode.php) & I cant find anything in the invision code which was designed my Martians (well designed I mean , but by aliens)

Mhtml
04-06-2003, 02:00 PM
I have the CHM version of the manual. I was looking all through it for stuff on regex, must have missed that example.

brothercake
04-06-2003, 02:36 PM
try this


<?

$mess = "visit www.codingforums.com";
$url = $mess;

echo ("<a href=\"http://" . preg_replace("/.+ w{3}/","www",$url). "\">$mess</a>");
?>


Which will match

'any number of characters' followed by " www" followed by 'anything'

and turn it into

<a href="http://wwwanything">any number of characters</a>

Mhtml
04-06-2003, 05:00 PM
Well that works brothercake but it will convert the whole string into a link no matter what.

The previously posted one works fine except for when the user doesn't enter http:// so I'll have to figure that bit out. :)

Mhtml
04-07-2003, 03:34 PM
Buh, no such luck. :( I tried for a while, I got close to making it add http:// onto www.domain.com addresses but I somehow kept repearing the address a few times and adding /http:// lol. :confused:

brothercake
04-07-2003, 04:12 PM
I assumed you want the string to match "anything folowed by www.domain.com"; if you can give more precise details of what you want it to match I could probably write the regexp for you

Mhtml
04-07-2003, 04:24 PM
I want it to match a url, or at least a string resembling it and convert that url into a hyperlink.

The current one I have is the one before you posted, it works fine with the exception that it doesn't work it you don't have http:// infront of your link..

IE www.domain.com becomes <a href="www.domain.com">www.domain.com</a> but needs to be href="http://www.domain.com">www.domain.com</a>

Am I explaining enough?

brothercake
04-17-2003, 04:00 PM
Sorry man - I can't work it out :( I suggest posting this again, as a regex problem

Mhtml
04-25-2003, 04:01 PM
WOOT! I have figured out my own shorter (for the time being) version of converting links.. .YAY!

Have been playing with regular expressions all day, all of a sudden a light bulb whent off over my head and I am no longer regex challenged. :D:p

I have to run a few more tests before I post the code but my initial and only test worked.

mordred
04-25-2003, 05:01 PM
hehe, welcome to the wonderful world of RegExps...
URLs are a hard and moving target for RegExps, because the definition what a URL (or URI) constitutes is rather broad, so it's usually quite a daunting task to create a RegExp that suits most possibilities... I often use simpler ones that leave the freedom to enter the occasional non-sensical urls.

Here's the URI matching RegExp of the offical german PHP newsgroup FAQ, provides perhaps some inspiration:



/**
* replace URIs with appropriate HTML code to be clickable.
*/
function replace_uri($str) {
$pattern = '#(^|[^\"=]{1})(http://|ftp://|mailto:|news:)([^\s<>]+)([\s\n<>]|$)#sm';
return preg_replace($pattern,"\\1<a href=\"\\2\\3\"><u>\\2\\3</u></a>\\4",$str);
}



BTW: And just one test means more or less nothing. Especially with RegExps.

Mhtml
04-25-2003, 06:21 PM
Lol ... I found that out pretty quickly. :)
I've gone through a quite a few different variations and currently I'm using:

preg_replace("/([^A-z0-9])(http|ftp|https)([\:\/\/])(.*)([^A-z0-9])/","<a href=\"$2$3$4\">$2$3$4</a>",$message);

I did have a better one but I'll have to figure out what it was because I can't remember.. .The problem is that it doesn't stop when it gets to a space in that regexp.:confused:

I think I'll start again.

Mhtml
04-25-2003, 07:04 PM
Work smarter, not harder!


$message = preg_replace("/([^A-z0-9])(http|ftp|https)([\:\/\/])([^\\s]+)/","<a href=\"$2$3$4\">$2$3$4</a>",$message);



EZ Archive Ads Plugin for vBulletin Copyright 2006 Computer Help Forum