Reputation: 97
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'
Upvotes: 5
Views: 2888
Reputation: 4637
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
Upvotes: 1
Reputation: 3915
I'd like better:
preg_match_all('/#([^#]+)#/msiU',$string,$matches);
to handle multiline sentences
Upvotes: 0
Reputation: 70480
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);
Upvotes: 15