Writing a forum - Parsing 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
alfone
Forum Newbie
Posts: 1
Joined: Wed Nov 22, 2006 5:31 pm

Writing a forum - Parsing links

Post by alfone »

NOTE: Square brackets [,] have been replaced with curly brackets {,} so that the forum does not parse my post.

I'm writing a barbones forum in PHP, and am having trouble figuring out how to parse the links from the user generated {url=http://www.domain.com}domain.com{/url} to the proper HTML of < a href="http://www.domain.com/" >domain.com< /a >.

The other stuff, {b}{i}{u} is easy, 'cause you can just run str_replace to switch out the user entered tags, to the html... but with the link, the domain is a wildcard and i need to grab it from between the "{url=" and the "}".

Does anybody know how to chnage {url=http://www.domain.com}domain.com{/url} to < a href="http://www.domain.com" >domain.com< /a > with a simple string function?

Thanks in advance for the help.
User avatar
Burrito
Spockulator
Posts: 4715
Joined: Wed Feb 04, 2004 8:15 pm
Location: Eden, Utah

Post by Burrito »

you can't do it with a 'simple string function'. You're going to need a regular expression to accommodate for variable information.

maybe something like:

Code: Select all

$pattern = "/\[url=\"(.*?)\"\](.*?)\[\/url\]/";
User avatar
feyd
Neighborhood Spidermoddy
Posts: 31559
Joined: Mon Mar 29, 2004 3:24 pm
Location: Bothell, Washington, USA

Post by feyd »

You'll need XSS protection code in there too, otherwise you'll have some real fun with hackers.
coolenta
Forum Newbie
Posts: 6
Joined: Wed Nov 22, 2006 7:22 pm

Post by coolenta »

{url=http://www.domain.com}domain.com{/url}
Im not shure if this is what you want but this is how i would do it

Code: Select all

$input = str_replace ('{url=', '<a href="', $input );
$input = str_replace ('}', '">', $input );
$input = str_replace ('{/url}', '</a>', $input );
User avatar
JayBird
Admin
Posts: 4524
Joined: Wed Aug 13, 2003 7:02 am
Location: York, UK
Contact:

Post by JayBird »

coolenta wrote:{url=http://www.domain.com}domain.com{/url}
Im not shure if this is what you want but this is how i would do it

Code: Select all

$input = str_replace ('{url=', '<a href="', $input );
$input = str_replace ('}', '">', $input );
$input = str_replace ('{/url}', '</a>', $input );
That's a great example of how really not to do it
User avatar
Ambush Commander
DevNet Master
Posts: 3698
Joined: Mon Oct 25, 2004 9:29 pm
Location: New Jersey, US

Post by Ambush Commander »

What you'll want to do is preg_replace_callback. The URI gets passed to the callback, who ampersand-escapes and makes sure that the scheme is sane (and not say javascript:), and then returns the final linkified URI.
User avatar
John Cartwright
Site Admin
Posts: 11470
Joined: Tue Dec 23, 2003 2:10 am
Location: Toronto
Contact:

Post by John Cartwright »

preg_quote() might also be of interest
Post Reply