Extracting Twitter hashtag from string in PHP

前端 未结 7 1151
离开以前
离开以前 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:45

    Extract multiple hashtag to array

    $body = 'My #name is #Eminem, I am rap #god, #Yoyoya check it #out';
    $hashtag_set = [];
    $array = explode('#', $body);
    
    foreach ($array as $key => $row) {
        $hashtag = [];
        if (!empty($row)) {
            $hashtag =  explode(' ', $row);
            $hashtag_set[] = '#' . $hashtag[0];
        }
    }
    print_r($hashtag_set);
    

提交回复
热议问题