Link being overwritten incorrectly when using preg_replace

Asked

Viewed 86 times

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 regexto url wouldn’t be ^(https?:\/\/)?([\da-z\.-]+)\.([a-z\.]{2,6})([\/\w \.-]*)*\/?

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

1 answer

0

You can use a lookbehind assertion to check whether the hashtag has a space before:

/(?<=\s)#(\w+)/

The substitution does not change because the groups started with (? are not captured

Browser other questions tagged

You are not signed in. Login or sign up in order to post.