递归方式解析出字符串中的@某人

<?php

$userList = [];
/**
 * 用递归的方式来查找字符串中的 @用户名
 * 用法:@用户名之后需要加空格隔断,才能检测到
 * @param $content
 */
function fetchAt($content)
{
    global $userList;
    $afterAt = strstr($content, '@'); //@符号之后
    if ($afterAt) {
        $username = strstr($afterAt, ' ', true); //用户名
        if ($username) {
            array_push($userList, mb_substr($username, 1));
            $rest = strstr($afterAt, ' '); //用户名之后的部分
            fetchAt($rest);
        } else {
            array_push($userList, mb_substr($afterAt, 1));
        }
    }
}

$content = '最近还好吗?@赵兴亚 ,@周星驰 好久没联系@林青霞';
fetchAt($content);
print_r($userList);

输出:Array ( [0] => 赵兴亚 [1] => 周星驰 [2] => 林青霞 )

原文地址:https://www.cnblogs.com/xingyazhao/p/7237101.html