Php @title系统无法生成正确的输出
以下函数未获得正确的结果:Php @title系统无法生成正确的输出,php,Php,以下函数未获得正确的结果: function getMentions($content) { global $db; $mention_regex = "/@+([a-zA-Z0-9-_]+)/"; //mention regrex to get all @texts $regexIt = preg_match_all($mention_regex, $content, $matches); if ($regexIt) { foreach ($
function getMentions($content) {
global $db;
$mention_regex = "/@+([a-zA-Z0-9-_]+)/"; //mention regrex to get all @texts
$regexIt = preg_match_all($mention_regex, $content, $matches);
if ($regexIt) {
foreach ($matches[1] as $key => $match) {
if ($key === 0) continue;
$mentioned[] = mysqli_real_escape_string($db, $match[0]);
$match_user = mysqli_query($db, "SELECT user_id, user_name FROM dot_users WHERE user_name IN ('" . implode("','", $matches[1]) . "')") or die(mysqli_error($db));
$userDeti = mysqli_fetch_array($match_user, MYSQLI_ASSOC);
echo $userDeti['user_id'];
echo $userDeti['user_name'];
$match_search = '@' . $match . '';
$match_replace = '<a target="_blank" href="' . $userDeti['user_name'] . '">@' . $userDeti['user_name'] . '</a>';
if (isset($userDeti['user_name'])) {
$content = str_replace($match_search, $match_replace, $content);
}
}
}
return $content;
}
你能告诉我我做错了什么或不完整吗?我未经检验的建议
function getMentions($content) {
global $db; // I would rather this be passed as a function argument
if (preg_match_all("/\B@\K[\w-]+/", $content, $matches)) {
if (!$result = mysqli_query($db, "SELECT user_id, user_name FROM dot_users WHERE user_name IN ('" . implode("','", $matches[0]) . "')")) {
// error
} else {
foreach ($result as $row) {
$content = preg_replace("~\B@{$row["user_name"]}\b~", "<a href=\"{$row["user_id"]}\">@{$row["user_name"]}</a>", $content);
}
}
}
return $content;
}
函数获取提及($content){
global$db;//我宁愿将其作为函数参数传递
如果(preg_match_all(“/\B@\K[\w-]+/”,$content,$matches)){
如果(!$result=mysqli_query($db,“从用户名位于(“.”中的点用户中选择用户id、用户名。”。内爆(“,”,$matches[0])。“)”){
//错误
}否则{
foreach($结果为$行){
$content=preg\u replace(“~\B@{$row[“user\u name”]}\B~”,“”,$content);
}
}
}
返回$content;
}
可能有拼写错误,但总的想法是存在的。捕获提及的内容并尽量避免电子邮件,查找ID,替换所有提及的内容。您在哪一行打印$userDeti['user\u name']我们需要一条实际的错误消息和明确的行号指示来帮助您。使用一种更简单的模式
~@\K[\w-]+~
这样您就可以处理完整的字符串匹配[0]
@rpm192中的输出警告:警告:为foreach()提供的参数无效。我没有收到任何错误消息。如果我收到错误消息,我会把它放在这里。@Azzo这是否真的运行了?我可以问你一些与你的答案相关的问题吗?在无间隙的文本中是否有这样的用户名:@mickmackusaisabesthelpfuluseronstackoverflow
。在本文中您可以看到,通常情况下,@mickmackusa是stackoverflow方面最有帮助的用户。但是没有空间。我们可以像这样定义用户名?您可以删除单词边界\b
和\b
来放松模式,但这样会有“过度匹配”的风险。想象一下@mick
和@mickmack
。根据更换的时间,您将生成嵌套的
标记。
function getMentions($content) {
global $db; // I would rather this be passed as a function argument
if (preg_match_all("/\B@\K[\w-]+/", $content, $matches)) {
if (!$result = mysqli_query($db, "SELECT user_id, user_name FROM dot_users WHERE user_name IN ('" . implode("','", $matches[0]) . "')")) {
// error
} else {
foreach ($result as $row) {
$content = preg_replace("~\B@{$row["user_name"]}\b~", "<a href=\"{$row["user_id"]}\">@{$row["user_name"]}</a>", $content);
}
}
}
return $content;
}