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.
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://
orftp://
.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.
orftp.
followed by one or more characters in the set[\w\#$%&~/.\-;:=,?@\[\]+]
.It should probably match URLs that start with
www.
orftp.
. Then the url / url scheme is added to the url.
a source to share
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.
a source to share