Results 1 to 4 of 4

Thread: Irregular Expressions

  1. #1
    Join Date
    Jul 2008
    Posts
    102
    Thanks
    36
    Thanked 6 Times in 6 Posts

    Question Irregular Expressions

    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;
    HTML Code:
    <a href="http://www.example">www.example-domain.com</a>
    PHP Code:
    $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.

    Please help
    Kind regards
    Dal
    Programmers are tools used to convert Caffeine to code

  2. #2
    Join Date
    Jan 2007
    Location
    Davenport, Iowa
    Posts
    2,413
    Thanks
    101
    Thanked 115 Times in 113 Posts

    Default

    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.

    PHP Code:
    $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.

  3. #3
    Join Date
    Jul 2008
    Posts
    102
    Thanks
    36
    Thanked 6 Times in 6 Posts

    Default

    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,

    http://www.programmingtalk.com/showthread.php?t=48191

    Kind regards
    Dal
    Programmers are tools used to convert Caffeine to code

  4. #4
    Join Date
    Jan 2007
    Location
    Davenport, Iowa
    Posts
    2,413
    Thanks
    101
    Thanked 115 Times in 113 Posts

    Default

    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.

Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •