Skip to content

Instantly share code, notes, and snippets.

@pajcho
Created April 30, 2014 13:18
Show Gist options
  • Save pajcho/fc9e7e8ee0b082cfbebc to your computer and use it in GitHub Desktop.
Save pajcho/fc9e7e8ee0b082cfbebc to your computer and use it in GitHub Desktop.
Turn hashtags in text into links (excludes hashtags already in links)
<?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;
echo preg_replace('/(?!<a[^<>]*>[^#]+)#(\b[-_\w]+\b)(?![^<]*<\/a>)/', '<a href="/tags/$1">#$1</a>', $str);
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment