2

有人可以向我解释为什么这个函数不返回用户标记的 id 吗?

function userid($name){ 
    $a_sql = mysql_query("SELECT * FROM utenti WHERE tag='$name' ") or die( mysql_error()); 
    $a_id= mysql_result($a_sql ,0,"id"); 
    return $a_id;  
    } 

$text= $_POST["text"]; 
$text = preg_replace('/@([a-zA-Z0-9]+)/e', htmlspecialchars(userid('$1')), $text);

为什么这种方法不起作用?

4

2 回答 2

1

您需要引用整个第二个参数以将结果评估为 php:

$text = preg_replace('/@([a-zA-Z0-9]+)/e', 'htmlspecialchars(userid($1))', $text);

您正在做的是调用函数htmlspecialchars并将userid其结果提供给preg_replace.

于 2013-03-11T17:38:38.437 回答
1

而不是使用preg_replace()使用preg_replace_callback()

http://php.net/manual/en/function.preg-replace-callback.php

这允许您传递替换参数通常所在的函数。


作为旁注,请不要使用已弃用的mysql_*功能。改为使用MySQLi 或 PDO和准备好的语句。

于 2013-03-11T17:33:12.947 回答