Link being replaced in the wrong way when using preg_replace

1

I have an array with regular expressions that I use to replace url's / hashtags on 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 $conteudo has a link, for example: link , it replaces correctly; if you have a forg followed by text, for example #boatarde it also replaces, however, if you have a link that has a hashtag, for example: link the replacement is as follows:

  

#topo "target=" _ blank "> link #topo

and only bold parts turn into links.

How to fix?

    
asked by anonymous 11.06.2015 / 22:00

1 answer

0

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

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

Replacement does not change since groups started with (? are not captured

    
17.06.2015 / 18:47