What is the fastest way to get all hash tags from a Twitter feed in PHP?

This may be a simple question, but what is the fastest (shorter lead time) to find specific words in the text.

Example: search for all words with hash tag in beginning of the word Input: #google bought #zagat today Output: google zagat 
+4
source share
3 answers
 /#[^ ]+/ 

You can use preg_match_all

 preg_match_all ( '/#[^ ]+/' , $subject, $matches ); 
+4
source

Separate the line with the # character, and then separate the space. http://php.net/manual/en/function.explode.php

This is a quick solution that I threw together:

  $str = '#google bought #zagat today'; $a = explode('#', $str); foreach ($a as $key=>$value) { if($a != "") { $b = explode(' ', $value); echo $b[0] . " "; } } // output: google zagat 

Another solution, one-time use:

  $str = '#google bought #zagat today'; foreach (explode(' ', $str) as $key=>$value) { if(substr($value, 0, 1) == '#') echo str_replace('#', '', $value) . " "; } // output: google zagat 
+1
source

Simple code:

 $matches = null; $returnValue = preg_match_all( '/#([^\s]+)/i', 'text#tag ' . PHP_EOL . '#tag5 #tagščřý continue', $matches ); var_dump($matches[1]); 

displays

 array 0 => string 'tag' (length=3) 1 => string 'tag5' (length=4) 2 => string 'tagščřý' (length=11) 

Regular matching of everything after the hash (including numbers and Unicode characters). It is case insensitive /i .

If you need space before - just add \s to the regular expression '/\s#([^\s]+)/i' , the output will be:

 array 0 => string 'tag5' (length=4) 1 => string 'tagščřý' (length=11) 
0
source

All Articles