View Full Version : Irregular Expressions

10-02-2008, 02:16 PM
I'm dumbstuck with regular expression (I cant see anything regular about it!)

I want to convert all URL's (eg "www.example.com") to html anchor href code and so far it worked until I came across www.example-domain.com where it failed at the hyphen ("-"). The code below outputs;

<a href="http://www.example">www.example-domain.com</a>

$HTML = eregi_replace('[^"/](www.[a-zA-Z0-9\.\/-/_\\&%=#]+)',' <a style="color:inherit;" href="http://\\1">\\1</a>',$HTML);

Just a couple of notes;

Note 1: I need to convert only www. references as http:// are not needed
Note 2: I can't convert all because images such as <img src="www.example.com/image.jpg"> would be converted to have <img src="<a href="www.example.com/image.jpg">www.example.com/image.jpg</a>"> in the middle.

The code above it not perfect and I can't yet implement BBCode for the job. I need a temp solution to the above problem but I cant make it get past the hyphen for some reason.

Please bare in mind that regular expression has always been my nemesis. :eek:

Please help
Kind regards

10-11-2008, 03:35 AM
I think that I know why your example www.example-domain.com was not matching. My guess is it was at the end of a sentence or subject string.

I have not been looking at your code, but am trying to create one of my own to do what you want. Here is what I came up with. Let me know if it does not work.

$text = preg_replace('/(?<!http:\/\/|\"|=)(www\..*?)(\s|\Z|\.\Z|\.\s)/i','<a href=\"$1\">$1</a>$2',$text);

I want to add that I am no expert in regex or PCRE as I like to call it since we are dealing with php here.

10-11-2008, 10:55 PM
Hi James(438)

please check at programmingtalk.com for the same thread title. I solved it there but didn't think to post the solution here cos I got no help. Thanks for sharing this with me and you should find the thread on there usefull, seems I just need to put the - character at the beginning.

Hold on, Ill find the url,


Kind regards

10-12-2008, 12:49 AM
Thanks for the link. I chuckled when I read the solution that you needed the hyphen at the beginning of the character class :)

I'd say Twey is the expert here on regular expressions. When I saw your question I was a bit intimidated by it, but finally decided to give it a go. Sorry about the long delay.