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!

Link to comment
Share on other sites

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);

Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • 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.