I have a php function that returns a Facebook post as text. However, I want all the #-hashtags to be clickable and to refer to http://www.facebook/hashtags/{the-hashtag}. I tried doing this with the following preg_replace, but apparently I'm doing something wrong:
$postMessage = preg_replace('/#[^(\s|\p{P})]*', '<a href="https://www.facebook.com/hashtag/$1" title="$1"></a>', $postMessage);
This outputs links where expected, so the regex seems right, but the output looks like this:
<a href="https://www.facebook.com/hashtag/" title=""></a>
So I'm pretty sure I'm doing something wrong with the back reference, but I'm not entirely sure what.
(Side question, is the global
parameter not necessary in preg_replace? I'm used to using it in JS.)
An example of $postMessage
:
Android Wear testen doen we met de Sony #Smartwatch3. Binnenkort volgt een uitgebreide review op de website ;-)
Output should be:
Android Wear testen doen we met de Sony <a href="https://www.facebook.com/hashtag/smartwatch3" title="Smartwatch3">#Smartwatch3</a>. Binnenkort volgt een uitgebreide review op de website ;-)