Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/php/233.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 - Fatal编程技术网

如何在PHP中解析用户提及的内容

如何在PHP中解析用户提及的内容,php,Php,我正在开发一个系统,其中用户以以下格式输入其消息: 嘿,约翰,你要参加elise的生日派对吗 当您在文本框中键入@时,下拉列表就会出现,您可以从中选择所需的名称 我的问题是,当发布消息时,它应该是这样的HTML代码 Hey <a href="http://www.mysite.com/user/id/123">John Doe</a> are you coming in birthday party of <a href="http://www.mysite.com

我正在开发一个系统,其中用户以以下格式输入其消息:

嘿,约翰,你要参加elise的生日派对吗

当您在文本框中键入@时,下拉列表就会出现,您可以从中选择所需的名称

我的问题是,当发布消息时,它应该是这样的HTML代码

Hey <a href="http://www.mysite.com/user/id/123">John Doe</a> are you coming in
birthday party of <a href="http://www.mysite.com/user/id/789">Elise Lisa</a>?

preg_replace("/@([A-Za-z0-9_]+)(?=\?|\,|\;|\.|\s|\Z)/", <new_html>, <old_string>);
嘿,你要进来吗
生日派对?
preg|u替换(“/@([A-Za-z0-9|]+)(?=\?\?\,\ \。\s\124;\ Z)/”,);
那么如何在字符串中找到所有出现的@并对其进行处理呢??因为我需要传递一个函数调用getUserName($shortname)和getUserID($shortname),其中$shortname是@之后的文本。此外,我还需要为每个短名称调用NotifyUser()函数。

您要使用它来遍历字符串,识别以“@”开头的所有单词,然后在这些单词上运行您的函数:

$text = 'say hello to @elise and @john';

function replace_at_symbol($matches){
    return "(here you would replace: ".$matches[0]." with something)";
}

$output = preg_replace_callback("/([@][a-zA-Z-0-9]+)/", "replace_at_symbol", $text);
echo $output;

对我来说似乎不是问题。你为什么有问题?