Retrieve All Hashtags from a Tweet in a PHP Function

Retrieve all hashtags from a tweet in a PHP function

$tweet = "this has a #hashtag a  #badhash-tag and a #goodhash_tag";

preg_match_all("/(#\w+)/", $tweet, $matches);

var_dump( $matches );

*Dashes are illegal chars for hashtags, underscores are allowed.

php extract all hashtags and twitter names from a block of text

preg_match_all solution:

$longstring = "blah blah blah #hashtag blah blah @twittername blah email@email.com blah blah #hashtag2 blah blah";
preg_match_all("/(?:^|\s)[#@][^ @#]+\b/", $longstring, $m);
$extracted = implode("", $m[0]);

print_r($extracted);

The output:

#hashtag @twittername #hashtag2

Retrieve all hashtags from a tweet

You'll have to use a regular expression in your language of choice

   #\S+

should match any hash, beginning with # and made of a string of characters. It stops at the first space.

If you want to exclude any trailing symbol and have a letter as the last char :

#\S*\w

This expression should work in most of the regex engines I'm aware of.

Extracting Twitter hashtag from string in PHP

Use preg_match() to identify the hash and capture it to a variable, like so:

$string = 'Tweet #hashtag';
preg_match("/#(\\w+)/", $string, $matches);
$hash = $matches[1];
var_dump( $hash); // Outputs 'hashtag'

Demo

Count all hashtags in a string - PHP

$string = 'This is a #0 and #1 works like #2';   // Result : 3
$string2= 'foo #bar baz######'; // Result : 1

preg_match_all("/(#\w+)/", $string, $matches);
echo count($matches[0]);

Edit:
You could then put it inside a function

function countHashTags($string)
{
if(preg_match_all("/(#\w+)/", $string, $matches))
return count($matches[0]);
else
return 0;
}

PHP : How to count the number of URLs, hashtags and user_mentions in a Twitter tweet

Based off of the answer in Retrieve all hashtags from a tweet in a PHP function, just adding the counting of the ouput.

$tweet = "this has a #hashtag a  #badhash-tag and a #goodhash_tag";
preg_match_all("/(#\w+)/", $tweet, $matches);
count($matches[0]);

Locate hashtags

To find all of the hash tags, use a regex and preg_match_all(), and do the replacement with preg_replace():

$regex = '/(#[A-Za-z-]+)/';
preg_match_all( $regex, $string, $matches);
$string_f = preg_replace( $regex, "<a href='#'>$1</a>", $string);

Then all of the tags are in an array in $matches[1]:

$tags_array = $matches[1];

Then, convert that to a space-separated list with implode() and array_unique():

$tags = implode( ' ', array_unique( $tags_array));

And you're done. You can see from this demo that $tags and $string_f are:

"#hashtag #another #example"
"Hello. This is a <a href='#'>#hashtag</a> and this is yet another <a href='#'>#hashtag</a>. This is <a href='#'>#another</a> <a href='#'>#example</a>."

For other characters in the hashtag (for example, digits), modify the $regex appropriately.

Edit: However, this can be improved in efficiency if you use preg_replace_callback() and a closure so you only have to execute the regex once, like so:

$tags_array = array();
$string_f = preg_replace_callback( '/(#[A-Za-z-]+)/', function( $match) use( &$tags_array) {
$tags_array[] = $match[1];
return "<a href='#'>" . $match[1] . "</a>";
}, $string);
$tags = implode( ' ', array_unique( $tags_array));

Find URLs, @replies and #hashtags from Tweets

I think what you're looking to do is essentially what I've included below. You'd add these two statements in transform method, just before the return statement.

$text = preg_replace('#@(\w+)#', '<a href="http://twitter.com/$1">$0</a>', $text);
$text = preg_replace('/#(\w+)/', '<a href="http://twitter.com/search?q=%23$1&src=hash">$0</a>', $text);

Is that what you're looking for?



Related Topics



Leave a reply



Submit