I am using this preg_replace to create hashtags twitter style: $text = preg_replace('/(^|\s)#(\w*[A-Za-z-_]+\w*)/', '<a href="?query&search=$2">#$2</a>', $text); Code (markup): The problem is it creates hashtags from everything else that has a # sign. So when someone enters an address. i.e.: Some St. Suit #b75 that b75 will become a hashtag. Sounds like an impossible task to tell it to differentiate between a real hashtag and something that is not. What's even worse, if it finds something like <span style="font-color: #FF0000;"></span> it will turn that font color into a hashtag too. So should I just throw that code out or is there a way to make it work?
Make the code check for hashtags only at the end of input-text, or create your own input for hashtags, and parse those instead? Hashtags should be at the end,at least in my book However, it might be very hard to differentiate completely, since something that looks like a hashtag might not be, and vice versa.