3

Possible Duplicate:
How to add anchor tag to a URL from text input
PHP Regular expression to match keyword outside HTML tag <a>

Alright, so I am using OsTicket (email ticket system), and have converted it to use HTML format emails going both in and out. It's hacked, but it works.

Well, at the moment there is a function in there called "clickableURLS"....

  function clickableurls($text) {

        //Not perfect but it works - please help improve it. 
        $text=preg_replace('/([^(\'|")]((f|ht){1}tp(s?):\/\/)[-a-zA-Z0-9@:%_\+.~#?&;\/\/=]+[^(\'|")])/','<a href="\\1" target="_blank">\\1</a>', $text);
        $text=preg_replace("/(^|[ \\n\\r\\t])([^('|\")]www\.([a-zA-Z0-9_-]+(\.[a-zA-Z0-9_-]+)+)(\/[^\/ \\n\\r]*)*[^('|\")])/",
                '\\1<a href="http://\\2" target="_blank">\\2</a>', $text);
        $text=preg_replace("/(^|[ \\n\\r\\t])([^('|\")][_\.0-9a-z-]+@([0-9a-z][0-9a-z-]+\.)+[a-z]{2,4}[^('|\")])/",'\\1<a href="mailto:\\2" target="_blank">\\2</a>', $text);


        return $text;
    }

As you can see, it basically takes a URL (searches for http or https or www) and then adds the appropriate mumbo jumbo in there.

WELL....

I now have a WYSIWYG set up.... so if a one of our techs usees the WYSIWYG to create a link the RIGHT way...which automatically creates the code.... then the clickableurls function ALSO finds the http that is already in the text and converts it AGAIN...

So I am basically trying to figure out the best way to handle that. I do want BOTH options.. and was thinking maybe doing an IF statement, but wouldnt changing the preg_replace make a bit more sense?

Any thoughts? Thanks again overflow community!

Community
  • 1
  • 1
PaulHanak
  • 729
  • 2
  • 9
  • 21
  • There are problems with you regexes. Without knowing specific details its hard to help you. –  Mar 10 '12 at 23:12
  • Do a search for "Linkify URL" - many answers there! ([Here's the one I use](http://stackoverflow.com/a/5291451/433790)). – ridgerunner Mar 11 '12 at 04:29

1 Answers1

3

I can try to help you translate what you have in the first preg_replace() regex.
Thats the best I can do because I do not know your intent. I've extrapolated some intent,
but its up to you to determine that.

When regex's are complex, block formatting helps to easily see through complexity.

([^(\'|")]((f|ht){1}tp(s?):\/\/)[-a-zA-Z0-9@:%_\+.~#?&;\/\/=]+[^(\'|")])

(                                    # Capture group 1
    [^'"]                                # Consume a character that is not single nor double quote
    (                                    # Capture group 2
      (?:ftp|https?):                       # Either ftp, OR http (optional s) string literal
      //                                    # Literl double forward shashes
    )                                    # End capt grp 2
    [-a-zA-Z0-9@:%_+.~#?&;/=]+           # 1 or more (greedy) of the characters in this class
    [^'"]                                # Consume a character that is not single nor double quote
)                                    # End capt grp 1