PHP Regular Expression

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • moazam
    New Member
    • Oct 2007
    • 11

    PHP Regular Expression

    Hello,
    I have clickable url function which convert text into clickable urls. All works fine except it fails in one condition. Here is the code

    Code:
    function make_clickable_urls($text) {
      $text = preg_replace("/(?<!<a href=\")((http|ftp)+(s)?:\/\/[^<>\s]+)/i", "<a href=\"\\0\" target=\"_blank\">\\0</a>", $text );
      $text = eregi_replace('([[:space:]()[{}])(www.[-a-zA-Z0-9@:%_\+.~#?&//=]+)',
        '\\1<a href="http://\\2" target="_blank">\\2</a>', $text);
      $text = eregi_replace('([_\.0-9a-z-]+@([0-9a-z][0-9a-z-]+\.)+[a-z]{2,3})',
        '<a href="mailto:\\1" target="_blank">\\1</a>', $text);
      return $text;
    }
    This function fails when using it founds this type of text.. <a href="http://url.com">http://url.com</a>

    some one told me to use "Put the "a href" part back in" I tried but not luck.

    Please help me
  • Dormilich
    Recognized Expert Expert
    • Aug 2008
    • 8694

    #2
    why do you use 3 successive RegEx replaces?

    Comment

    • moazam
      New Member
      • Oct 2007
      • 11

      #3
      to convert following types of URLs

      URL.com is a sought-after domain that MediaOptions can help broker for you. Contact us today to get the domain name your company needs!

      URL.com is a sought-after domain that MediaOptions can help broker for you. Contact us today to get the domain name your company needs!

      bytme@bytes.com

      Comment

      • TheServant
        Recognized Expert Top Contributor
        • Feb 2008
        • 1168

        #4
        I'm with Dormilich. The easiest way (maybe not shortest or most efficient) is to simply have a regex replace for each different form of url someonw may enter. Alternatively, can you search for "[a-zA-Z0-9].[a-z](2,3)" and then find any preceeding text and then process it accordingly? All url's have the domain name and .com/.net/etc so if you really want to be tricky that would be one way.

        Comment

        • moazam
          New Member
          • Oct 2007
          • 11

          #5
          I think you are not getting it.. these urls are in a document where I allow HTML however some users do not use html because they do not so they put 2 types URL mentioned above as well as email which i convert by using make_clickable_ urls() this function works 100% except it fails on this condition if following text found in a document

          This is long paragraph http://www.url.com, this is HTML url <a href="http://www.url.com">ht tp://www.url.com</a>

          it ignores the a href but it looks inside http://..... and convert them to HTML this is my problem

          Comment

          • pwfisher
            New Member
            • Mar 2009
            • 1

            #6
            To take any random html and reliably detect un-linked URLs requires a full parsing engine. Simple regular expressions will always fail on complex cases like

            Code:
            <a href="http://example.com">this link has a <b>bold</b> word in it, and points to http://example.com</a>
            Simple solution: strip_tags().

            Tweak to fix your immediate problem:

            Code:
            $text = preg_replace('#(?<![">])((http|ftp)s?://[^<>\s]+)#i', '<a href="\\0" target="_blank">\\0</a>', $text );
            It's a fragile solution, but may be good enough. It makes urls into links as long as they're not preceded by a " or >.

            Comment

            • moazam
              New Member
              • Oct 2007
              • 11

              #7
              pwfisher you are great.. this worked excellent..
              Code:
              $text = preg_replace('#(?<![">])((http|ftp)s?://[^<>\s]+)#i', '<a href="\\0" target="_blank">\\0</a>', $text );
              Btw: There is no need of strip_tags :)

              Thanks

              Comment

              Working...