I want to retrieve all hashtags from a tweet using a PHP function.
I know someone asked a similar question here, but there is no hint how exactly to implement this in PHP. Since I'm not very familiar with regular expressions, don't know how to write a function that returns an array of all hashtags in a tweet.
So how do I do this, using the following regular expression:
#\S*\w
Use
twitter-text-php
package.https://github.com/nojimage/twitter-text-php
https://packagist.org/packages/nojimage/twitter-text-php
Try this regular expression:
Running it PHP would look like:
The result is an array containing all the hashtags in the Tweet (saved as "$result" - the third argument).
Lastly, check out this site. I've found it really handy for testing regular expressions. http://regex.larsolavtorvik.com/
EDIT: I tried your regular expression and it worked great too!
I created my own solution. It does:
Supports unicode characters
Output is like this:
*Dashes are illegal chars for hashtags, underscores are allowed.
Don't forget about hashtags that contain unicode, numeric values and underscores:
\p{Pc} - to match underscore
\p{N} - numeric character in any script
\p{L} - letter from any language
\p{Mn} - any non marking space (accents, umlauts, etc)
Use the
preg_match_all()
function: