1

我的问题是retrieveName() 没有获得$1 的值,但$1 在前一个实例中工作得很好。

    function bbcode ($string)
    {
    // All the default bbcode arrays.
    $bbcode = array(
    '#\[quote=(.*?)\](.*?)\[/quote\]#si' => 
'<span class="bbcode_quote"><b>
<a href="userprofile.php?id='.stripslashes('$1').'" target="_blank">
<span class="fake_link">'.retrieveName('$1').'</span></a> Said:</b><BR/>$2</span><BR/>'

    );
    $output = preg_replace(array_keys($bbcode), array_values($bbcode), $string);
    $output = str_replace("\\r\\n", "<br>", $output);
    return $output;
    }

编辑: 没有斜线,我希望它是那么简单

function retrieveName($poster_id){
$get_name = mysql_query("SELECT * FROM users WHERE userid = 'sanitizeIn($poster_id)'")
or die(mysql_error());
$name_row = mysql_fetch_array($get_name);
return $name_row['username'];
}
function sanitizeIn ($string) {
$output = mysql_real_escape_string($string);
return $output;
}
4

2 回答 2

0

Assuming you are using preg_* functions, as you should be, you should use $1 instead of \\1. Both are valid, but $1 is the preferred syntax.

Also, you might be more interested in one of the following:

$output = preg_replace("#\[quote=(.*?)\](.*?)\[/quote\]#sie",  
  "'<span class=\"bbcode_quote\"><b><a href=\"userprofile.php?id='.stripslashes('$1').'\"
    target=\"_blank\"><span class=\"fake_link\">'. 
   retrieveName(stripslashes('$1')) . '</span></a> Said:</b><BR/>$2 
  </span><BR/>'",$input);

Or:

$output = preg_replace_callback("#\[quote=(.*?)\](.*?)\[/quote\]#si",function($m) {
    return '<span class="bbcode_quote"><b><a href="userprofile.php?id=\\1"  
    target="_blank"><span class="fake_link">' .  
   retrieveName('\\1') . '</span></a> Said:</b><BR/>\\2 
  </span><BR/>';
},$input);
于 2012-07-09T05:10:09.123 回答
0

试试这个方法:

$output = preg_replace_callback("#\[quote=(.*?)\](.*?)\[/quote\]#si",function($matches) {
    return '<span class="bbcode_quote"><b><a href="userprofile.php?id='.stripslashes($matches[1]).'" target="_blank"><span class="fake_link">' .  
    retrieveName($matches[1]).'</span></a> Said:</b><BR/>'.$matches[2].'</span><BR/>';
},$input);
于 2012-07-09T05:34:12.667 回答