What do these regex patterns match?

I am new to regex in PHP and understand the basic patterns, but the ones below are a bit tricky and I don't understand what the following pattern matches:

$ret = preg_replace("#(^|[\n ])([\w]+?://[\w\#$%&~/.\-;:=,?@\[\]+]*)#... "<a href='' rel='nofollow'></a>", $ret);

$ret = preg_replace("#(^|[\n ])((www|ftp)\.[\w\#$%&~/.\-;:=,?@\[\]+]*... "<a href='http://' rel='nofollow'></a>", $ret);

      

Can someone explain them?

Thanks.

+1


a source to share


2 answers


In short: Replace URLs on links.

More details:



  • The first regular expression describes sequences starting with word characters ( [\w]+

    ), ://

    followed by one or more characters in the set [\w\#$%&~/.\-;:=,?@\[\]+]

    .

    This probably corresponds to the URL-address, starting with the protocol / URL schemes, e.g. http://

    , https://

    or ftp://

    .

    But it will also fit javascript://

    . And that's not good: javascript://%0Aalert%28%22booo%21%22%29

    equal to JavaScript code:

    //
    alert("booo!")
    
          

  • The second regular expression describes sequences that begin with www.

    or ftp.

    followed by one or more characters in the set [\w\#$%&~/.\-;:=,?@\[\]+]

    .

    It should probably match URLs that start with www.

    or ftp.

    . Then the url / url scheme is added to the url.

+3


a source


Get RegexBuddy and it explains to you ( see screenshots ) what any regex means. There is another anwser here on SO that demonstrates this .



In any case, according to the second preg_replace

s argument , they must match the urls and tag them.

+2


a source







All Articles