Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/php/282.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/4/regex/16.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
检测php中以逗号连接的提及_Php_Regex - Fatal编程技术网

检测php中以逗号连接的提及

检测php中以逗号连接的提及,php,regex,Php,Regex,我有这样一个字符串: *"Hola @user1,@user2. ¿Que tal ayer con@user3 en #el_parque?"* 我想提取@提及的内容,并分析用户是否存在。如果不存在,我将删除@标记 如何解析字符串 我开发了这个函数 if(!empty($Timeline)){ $CI =&get_instance(); $words = explode(" ", $Timeline); $renderTimeline

我有这样一个字符串:

*"Hola @user1,@user2. ¿Que tal ayer con@user3 en #el_parque?"*
我想提取
@提及的内容
,并分析用户是否存在。如果不存在,我将删除
@
标记

如何解析字符串

我开发了这个函数

if(!empty($Timeline)){
        $CI =&get_instance();

        $words = explode(" ", $Timeline);
        $renderTimeline = "";

        $patternu = '@([a-zA-Z09]+)';
        $patternm = '/([#]{1})([a-zA-Z0-9\_]+)/';

        if(!isset($CI->usuarios_model)){$CI->load->model('usuarios_model');}
        if(!isset($CI->juegos_model)){$CI->load->model('juegos_model');}

        foreach($words as $word){
            if(preg_match($patternu,$word)){ //mencion
                $word = preg_replace($patternu, '$0', $word);
                echo $word;
                $usuario = $CI->usuarios_model->get(null,preg_replace($patternu, '\2', $word));
                if(!$usuario){$word = substr($word,1);}
            }elseif(preg_match($patternm,$word)){ //juego
                $juego = $CI->juegos_model->existJuego(null,preg_replace($patternm, '\2', $word));
                if(!$juego){$word = substr($word,1);}
            }

            $renderTimeline.=" ".$word;
        }

        return trim($renderTimeline);
    }

您可以使用正则表达式来实现这一点,类似于“@([a-zA-Z09]+)”的东西应该可以做到这一点。稍后,如果您的用户不存在,只需将
matcher.group()
替换为
matcher.group(1)
。第一个包含整个捕获字符串,其中包含
@
,第二个包含第一个组(例如,用户名不包含
@

请检查:

$string =  "Hola @userA,@userB. ¿Que tal ayer con@userC en #el_parque?" ;
$matches = array() ;
$pattern = "/@[a-zA-Z0-9]+/" ;

preg_match_all($pattern, $string, $matches);

var_dump($matches[0]);

也是con@user3在您的情况下,或者仅@user1和@user2?如何使用我提供的正则表达式字符串提取用户名字符串进行验证。检查以下正则表达式教程:在客户端执行(无需发布)或使用php在服务器端验证用户。如果我喜欢在用户名中加上“389;”(下划线),您需要熟悉正则表达式(对于抽象,对于JS或PHP)。正则表达式应该是这样的吗<代码>$pattern=“/@[a-zA-Z0-9][+/”它应该是:
“/@[a-zA-Z0-9!+/”