Jump to content

[SOLVED] More problems with that URL regex.


JasonLewis

Recommended Posts

Okay, some may remember, some may not. Basically I have this pattern, provided by effigy.

 

preg_match_all('%
      (
         (?>
            ### Protocol or start.
            (?:
               (??:https?|ftp)://)
               |
               www\.
            )
            ### Body: gobble everything except [/url]
            (??!\[/url\]|\[/img\]).)+
            ### Avoid ending punctuation.
            (?<!\s\p{P})
         )
         ### Not followed by an url end.
         (?!\[/url\]|\[/img\])
      )
      %x',$str,$matches);

 

It works, brilliantly. But now I've found another flaw in it.

Basically, what it does is links any unlinked links in a string. Like what this forum does. But now it's not working if I provide a link like this:

 

www.google.com < link that

 

Basically anything followed by a space/new line won't work. I don't know why it didn't happen last time I was testing it but now it's not working again. If you have a space it will link everything after the space. It's really odd, I cannot for the life of my figure out the problem. -_-"

 

Thanks all!

At some point the . was \S to only capture non-whitespace. I can't figure out why this changed in the old posts, but that's what you're after. I've restored this and cleaned up the img addition with alternation:

 

preg_match_all('%
      (
         (?>
            ### Protocol or start.
            (?:
               (??:https?|ftp)://)
               |
               www\.
            )
            ### Body: gobble non-space, [/url], [/img]
            (??!\[/(?:url|img)\])\S)+
            ### Avoid ending punctuation.
            (?<!\s\p{P})
         )
         ### Not followed by an url/img end.
         (?!\[/(?:url|img)\])
      )
      %x', $str, $matches);

Archived

This topic is now archived and is closed to further replies.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.