Extracting Twitter hashtag from string in PHP

前端 未结 7 1153
离开以前
离开以前 2020-12-18 11:19

I need some help with twitter hashtag, I need to extract a certain hashtag as string variable in PHP. Until now I have this

$hash = preg_replace (\"/#(\\\\w         


        
相关标签:
7条回答
  • 2020-12-18 11:49

    Here's a non Regex way to do it:

    <?php
    
    $tweet = "Foo bar #hashTag hello world";
    
    $hashPos = strpos($tweet,'#');
    $hashTag = '';
    
    while ($tweet[$hashPos] !== ' ') {
     $hashTag .= $tweet[$hashPos++];
    }
    
    echo $hashTag;
    

    Demo

    Note: This will only pickup the first hashtag in the tweet.

    0 讨论(0)
提交回复
热议问题