I am trying to extract the content following a hashtag using php.
For example:
$sentence = 'This #Coffee is the #best!!';
How do I get the value 'Coffee' and 'best'? Note, I don't want the exclamation mark after 'best'
The different words of a hashtag should be distinguished by using capital letters. For instance, a very popular hashtag campaign by Red Bull #PutACanOnIt is basically “Put a can on it.” The use of capital letters makes it easier to distinguish the different words of a hashtag.
You can also: Search for a hashtag using Facebook's search bar. Click on a hashtag to see a feed of Facebook posts using that same hashtag. Search hashtags used in private Facebook groups using the “search this group” bar under the group's menu.
A pretty safe catch-all in utf-8/unicode:
preg_match_all('/#([\p{L}\p{Mn}]+)/u',$string,$matches);
var_dump($matches);
Although, if you're not using / expecting exotic characters, this might work equally well and is more readable:
preg_match_all('/#(\w+)/',$string,$matches);
Try this one:
|\#(\w*)|
Run in a sentence like
I want to get all #something with #this
It will retrieve "something" and "this". Im assuming that you only wanted the Regex, the function to use is preg_match_all
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With