我的代码:
$string = "@admin BLA BLA @mark BLA BLA BLA @koko BLA BLA";
preg_match_all("/(@\w+)/", $string, $matches);
foreach ($matches[0] as $usernames) {
$user = (" SELECT * FROM user WHERE username = '" . $usernames . "' ");
while ($row = mysql_fetch_array($user)) {
$string = str_replace($usernames, $row["userid"], $string);
}
}
echo $string;
我正在尝试将usernames
之后的所有@
替换为其关联的用户ID,但是当我打印文本时,输出与原始文本相同。
输出:
@admin BLA BLA @mark BLA BLA BLA @koko BLA BLA
答案 0 :(得分:0)
您的SQL查询将类似于SELECT * FROM user WHERE username = '@admin'
- 这可能不是您的期望。
尝试此修复:
$user = (" SELECT * FROM user WHERE username = '" . substr($usernames, 1) . "' ");
希望它可以帮助你:)