Reputation: 781
I'm using this code to make hashtags:
preg_replace( '/\#([A-Za-z0-9]*)/is', ' <a href="tag/$1"> #$1</a> ', $text);
It works, but when it comes across this: "it#039;s" ( "it's" ), it replaces it as well. So I guess I want it to only replace hashtags if they have a white space before them, or are at the beginning of the line. Something like in this string below:
This is a #tag and this is #anotherTag but the word "it's" and "it#039;s" is not being replaced
How do I change the preg to do this? Thanks
Upvotes: 0
Views: 1345
Reputation: 2326
The concept you're looking for is a negative lookbehind
/(?<!=|\b|&)#([a-z0-9_]+)/i
Notice the ?<!=
part, this is saying match the hashtags where there isn't a \b
or #
in front of it.
Upvotes: 1