THE ZEPINT NETWORK

programmer assist

PHP PHP XML Feeds

PHP Questions PHP Solutions PHP Articles

PHP is a popular open-source programming language used primarily for developing server-side applications and dynamic web content, and more recently, other software. The name is a recursive acronym for "PHP: Hypertext Preprocessor".

URL's in PHP strings

DiggBlinkRedditDeliciousTechnorati

question by Srirangan | Moderate

Hello

I have a large text attributes stored in a Mysql DB after performing the htmlentities( .. , ENT_QUOTES) operation. Now some of these may have Url's in them like "http://.....".

How do I parse this text blob to convert the URL's into working hyperlinks? Thanks!

- Srirangan

Post reply Subscriptions

Re: URL's in PHP strings

reply by Anurag

Regular Expressions to the rescue. Use ereg_replace(slower) or preg_replace(faster)

You need to add an anchor tag to every http://... occurence

http://worldwideweb/ becomes <a href="http://worldwideweb/">http://worldwideweb/</a>

This will link upto a whitespace

$goodText = preg_replace('#(http://)([^\s]*)#', '<a href="\\1\\2">\\1\\2</a>', $badText);

$goodText = clickable links
$badText = unclickable links

Post reply Subscriptions

Re: URL's in PHP strings

reply by Srirangan

Thanks for the regex code. That works in most cases but not when there is a newline or <br/> tag. Any ideas how to make it work then?

Post reply Subscriptions

Re: URL's in PHP strings

answer by Anurag

[^...] means all characters except ...

[^(\s<)] just put all characters you want to exclude in parantheses like (\s<)

So you new format string becomes

#(http://)([^(\s<)]*)#

Post reply Subscriptions

Re: URL's in PHP strings

reply by Srirangan

Thanks! That worked perfect. :-)

Post reply Subscriptions

Re: URL's in PHP strings

reply by lexxwern

Interesting piece of regex code Anurag. Do you have a link for the docs on regular expressions for PHP?

Post reply Subscriptions

Re: URL's in PHP strings

reply by lexxwern

Anurag, please post the relevant link mate. Thanks!

Post reply Subscriptions

Re: URL's in PHP strings

reply by lexxwern

Hey, ignore my previous message. I got a good source: http://www.regular-expressions.info

Post reply Subscriptions

Re: URL's in PHP strings

reply by Anurag

yeah thats a nice site for regex

Post reply Subscriptions

Re: URL's in PHP strings

reply by Srirangan

hey anurag, this regex code get's buggy when the link is followed by a '<' character. any suggestions for that?

- sri

Post reply Subscriptions

Re: URL's in PHP strings

reply by VGR

regexp are always buggy to the bones, and they're unreadable, unmaintainable and sluggish.

you shouldn't do an html_entities() as there is no way out (I struggled once in a while with html_entity_decode() just to find it almost unuseable)

Doing simply an addslashes() is enough to store in the DB, and when reading after the stripslashes() you're done and your links do work as expected.

I suggest you add an HTML filter to prevent malicious tags to be embedded, for instance via strip_tags()

I have various "HtmlClean" - like functions for storing in a DB, so if you need one you know where to ask ;-)

bye

Post reply Subscriptions

Got a PHP Question?

Just Sign Up and ask the top PHP experts!

Search via Google

User Login

Email Address

Password

PHP Experts

Rank Expert Points
#1 Srirangan 2650
#2 Anurag 600
#3 mastercomputers 100
This a list of the Top PHP experts, how many points do you have?

Leading Experts

Rank Expert Points
#1 frankzzsword 4600
#2 Bejaan 2900
#3 csfreak 1100
#4 Anurag 700
#5 keyvez 700
#6 nnarasimha 600
#7 Nakata 600
#8 martinig 600
#9 mastercomputers 400
#10 Huntress 150
#11 Adkron 150
#12 Yogesh 100
#13 lexxwern 100
#14 Mustan Khan 100
#15 poizn 100
This is a list of overall best performing experts, how many points do you have?