To handle HTML editing, regular expressions are not the best Hithithite:
is more suitable solutions based on html/xml parsers.
However, maintaining the approach of the original question:
$reply = preg_replace('~(?<![\'"])(https?://\S+(gif|jpe?g|png))~i', ## jpeg -> img
"<img src='$1'>", $reply);
$reply = preg_replace('~(?<![\'"])(https?://\S+)~', ## http -> a href
"<a href='$1' target='_blank'>$1</a>", $reply);
$reply = preg_replace('~(?<![\'"/])(www.\w\S+)~', ## www.x -> a href
"<a href='http://$1' target='_blank'>$1</a>", $reply);
EDIT 1: (removed because it is contained in issue 2)
EDIT2:
The replacement of URL
for <f href='URL'>URL</a>
, only occurs if URL does not
is following '
or "
.
To deal with previously noted link situations and other situations not to note,
I added:
- a mark
''PrOtEcT''
before the address (as a slope '
to the address, this
will not be noted)
- in the end we remove the mark
that is to say:
$contexto_esq = ' <a\s.*?>\s* # proteger após <a...>
| " # proteger após ",
';
$txt= preg_replace("~($contexto_esq)( https? | www. )~ix", # add ''PrOtEcT''
"$1''PrOtEcT''$2", $txt);
$txt= preg_replace('~(?<![\'"])(https?://\S+(gif|jpe?g|png))~i', # jpeg -> img
"<img src='$1'>", $txt);
$txt= preg_replace('~(?<![\'"])(https?://\S+)~', # http -> a href
"<a href='$1' target='_blank'>$1</a>", $txt);
$txt= preg_replace('~(?<![\'"/])(www.\w\S+)~', # www. -> a hreg
"<a href='http://$1' target='_blank'>$1</a>", $txt);
$txt= preg_replace("~''PrOtEcT''~","",$txt); # remove ''PrOtEcT''
Test policy As the conditions of the exercise are not defined and are constantly changing,
it is crucial to use a set of test examples (which ideally should
be in the set)
$txt = <<<EOD
1) anotar com <a>:
http://n.u.pt/ mais www.di.br e http://www.di.br
2) não anotar:
"http://n.u.pt/sss1" e 'www.di.br' e 'http://www.di.br'
3) anotar com <img>:
http://n.u.pt/sss2.jpg e ainda http://www.di.br/dir/f.png
4) não anotar:
"http://n.u.pt/sss3.jpg" e ainda 'http://www.di.br/dir/f.png'
5) já anotado:
<a href="http://n.u.pt/" target="_blank">http://n.u.pt/</a>
6) já anotadao
<a href="http://n.u.pt/" target="_blank"> http://n.u.pt/f.jpg</a>
7) não anotar:
href="http://www.yoble.com.br/Community/434"> <img
EOD;
In the present case it is producing:
1) anotar com <a>:
<a href='http://n.u.pt/' target='_blank'>http://n.u.pt/</a> mais <a href='http://www.di.br' target='_blank'>www.di.br</a> e <a href='http://www.di.br' target='_blank'>http://www.di.br</a>
2) não anotar:
"http://n.u.pt/sss1" e 'www.di.br' e 'http://www.di.br'
3) anotar com <img>:
<img src='http://n.u.pt/sss2.jpg'> e ainda <img src='http://www.di.br/dir/f.png'>
4) não anotar:
"http://n.u.pt/sss3.jpg" e ainda 'http://www.di.br/dir/f.png'
5) já anotado:
<a href="http://n.u.pt/" target="_blank">http://n.u.pt/</a>
6) já anotadao
<a href="http://n.u.pt/" target="_blank"> http://n.u.pt/f.jpg</a>
7) não anotar:
href="http://www.yoble.com.br/Community/434"> <img
Your solution has some problems: (1) when the url is at the end of the line; (2) www.... generates invalid links; (3) generates double target=...;
– JJoao
You can give examples of strings you have at source and how you want them to appear later?
– Sergio