3v4l.org

run code in 150+ php & hhvm versions
Bugs & Features
<?php $str = <<<STR This is <a href="http://foo.com">This is #foo</a>. Simple #html-stuff text <a href=\"http://foobar.com#baz\">#simple</a> <span class="simple">simple #simple text text</span> STR; $str = preg_replace('/(?!<a[^<>]*>[^#]+)#(\b[-_\w]+\b)(?![^<]*<\/a>)/', '<a href="/tags/$1">#$1</a>', $str); echo $str;
Output for 4.3.0 - 5.6.28, hhvm-3.10.0 - 3.13.2, 7.0.0 - 7.1.0
This is <a href="http://foo.com">This is #foo</a>. Simple <a href="/tags/html-stuff">#html-stuff</a> text <a href=\"http://foobar.com#baz\">#simple</a> <span class="simple">simple <a href="/tags/simple">#simple</a> text text</span>