1
I have a regular expression array that I use to replace url’s/hashtags in links using preg_replace
:
$regs = array('!(\s|^)((https?://|www\.)+[a-z0-9_./?=;&#-]+)!i', '/#(\w+)/');
$subs = array(' <a href="$2" target="_blank">$2</a>', '<a href="/hashtag/$1" title="#$1">#$1</a>');
$saida = preg_replace($regs, $subs, $conteudo);
If, the $conteudo
have a link, for example: https://www.google.com/, it replaces correctly; if you have a hastag followed by text, for example #nightclub also replaces, however, if you have a link that has a hashtag, for example: https://www.google.com/#top the replacement is as follows::
#top" target="_Blank">https://www.google.com/#top
only the parts in bold are links.
How to fix?
the
regex
to url wouldn’t be^(https?:\/\/)?([\da-z\.-]+)\.([a-z\.]{2,6})([\/\w \.-]*)*\/?
– Maicon Carraro
I’m using the one I got in another question here at Stackoverflow, @Maiconcarraro. I’m a beginner in
regex
, however, the one I am using works properly, taking away the problem in question...– Igor