php function cannot call the value of the regular expression
怪我咯
怪我咯 2017-05-16 13:00:47
0
3
455

To solve the problem, as in the question, the goal is to read the user ID marked in the content and retrieve the user name from the database.
Problem, only "$1" can be read out, but the value cannot be obtained.

function usvid($csse,$uvid){
    global $db,$DBprefix;
    echo $csse.$uvid; $query_usersid="select * from ".$DBprefix."users where Uid='$csse' order by Uid desc"; $sql_usersid=mysql_query($query_usersid); while ($usersid=mysql_fetch_array( $sql_usersid)){
    $bbs_H=$usersid['UserName']; }
    return $bbs_H;
    }

function vubb($str){
    $str=preg_replace("/\[userid=(.+?)\](.+?)\[\/userid\]/is",usvid("$1",'$2'),$str);
    return $str;
    }
    
    echo vubb("[userid=10000]xy[/userid]");
怪我咯
怪我咯

走同样的路,发现不同的人生

reply all(3)
巴扎黑
  1. The second parameter of preg_replace can only be a string or array, the function usage is wrong

  2. Change to the correct preg_replace_callback, the second parameter is the callback function

Rewrite the example as follows (not tested whether it is correct or not):

function usvid($match) // 被修改
{
    $csse = $match[1]; // 新增行
    $uvid = $match[2]; // 新增行
    
    global $db, $DBprefix;
    echo $csse.$uvid; 
    $query_usersid = "select * from ".$DBprefix."users where Uid='$csse' order by Uid desc";
    $sql_usersid = mysql_query($query_usersid); 
    while ($usersid = mysql_fetch_array($sql_usersid)) {
        $bbs_H = $usersid['UserName'];   
    } 
    return $bbs_H; 
}

function vubb($str)
{
    $str = preg_replace_callback("/\[userid=(.+?)\](.+?)\[\/userid\]/is", "usvid", $str); // 修改行
    return $str; 
} 
    
echo vubb("[userid=10000]xy[/userid]");

Only 4 lines have been changed. The author can try again. Please forgive me if there are any mistakes.

刘奇

Thanks for the invitation!

Then, are there any questions?

My interpretation of your sql is:

    select * from users where Uid='' order by Uid desc
巴扎黑
function usvid($csse,$uvid) // 被修改
{
    global $db, $DBprefix;
    $query_usersid = "select * from ".$DBprefix."users where Uid='$csse' order by Uid desc";
    return  $query_usersid;
    $sql_usersid = mysql_query($query_usersid); 
    while ($usersid = mysql_fetch_array($sql_usersid)) {
        $bbs_H = $usersid['UserName'];   
    } 
    return $bbs_H; 
}

function vubb($str)
{
    $str = preg_replace("/\[userid=(.+?)\](.+?)\[\/userid\]/is", usvid("",''), $str); // 
} 
    
echo vubb("[userid=10000]xy[/userid]");

I tested it and the output sql is the same as what you requested
select * from users where Uid='10000' order by Uid desc

Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template