phpregextext-extractionhashtag

Get all hashtags from a string


I have this little code to extract the #hashtags:

$text = 'The standard chunk of Lorem Ipsum used since the 1500s is reproduced below for those interested. #lorem #ipsum #another#tags';
$content  = explode(' ', $text);
$tags = array();
foreach ($content as $item){
    if (preg_match('/#([^\s]+)/', $item, $matches)) {
        $tags[]= $matches[0];
    }
}

And got this:

Array(
    [0] => #lorem
    [1] => #ipsum
    [2] => #another#tags
)

The question is: How I can match even the #another#tags and append to my current array?

New trouble: some text has urls like http://someurl.com/here.html#top and the #top part is parsed like a tag too. Is there any way to avoid that?


Solution

  • If what you're trying to do is just pull out all of the hash tags, then you can simply do this:

    $text = 'The standard chunk of Lorem Ipsum used since the 1500s is reproduced below for those interested. #lorem #ipsum #another#tags';
    preg_match_all("/\#\\w+/", $text, $matches);
    $tags = $matches[0];