How to extract hashtag content?

I am trying to extract content after hashtag using php.

For instance:

$sentence = 'This #Coffee is the #best!!'; 

How do I get the value "Coffee" and "Best"? Notice I do not want an exclamation mark after the "best"

+4
source share
3 answers

Safe enough catch-all in utf-8 / unicode:

 preg_match_all('/#([\p{L}\p{Mn}]+)/u',$string,$matches); var_dump($matches); 

Although, if you are not using / not expecting exotic characters, this may work equally well and is more readable:

 preg_match_all('/#(\w+)/',$string,$matches); 
+14
source

Try the following:

 |\#(\w*)| 

Run in a sentence like

I want to get all #something c # this

It will retrieve "something" and "this". Assuming you only need Regex, the function used for this is preg_match_all

+1
source

I would like better:

 preg_match_all('/#([^#]+)#/msiU',$string,$matches); 

for processing multi-line sentences

0
source

All Articles