2012-07-09 18 views
1

我的问题是retrieveName()没有获得$ 1的值,但$ 1在前面的实例中工作得很好。排列中的PHP函数

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; 
} 
+1

可能需要双转义,转换为“\\\\ 1”,取决于retrieveName()中完成多少级别的处理 – 2012-07-09 04:44:38

+0

执行所有通过的变量编辑retrieveName以\\开头?如果他们这样做,为什么不传递整数值(1),然后在retrieveName函数内附加'\\'? – nbsp 2012-07-09 04:45:21

+0

看里面'retrieveName'使用可能使用'stripslashes' – Rab 2012-07-09 04:50:32

回答

0

假设你正在使用preg_*功能,你应该,你应该使用$1而不是\\1。两者都是有效的,但$1是首选语法。

而且,你可能更喜欢下面的一个:

$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); 

或者:

$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); 
+0

嘿,谢谢。我在第一篇文章中做了相应的修改。但是$ 1仍然没有将值传递给retrieveName(); – Mike 2012-07-09 05:23:51

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);