search

Home  >  Q&A  >  body text

php function cannot call the value of the regular expression

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]");
怪我咯怪我咯2797 days ago421

reply all(3)I'll reply

  • 巴扎黑

    巴扎黑2017-05-16 13:02:47

    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.

    reply
    0
  • 怪我咯

    怪我咯2017-05-16 13:02:47

    Thanks for the invitation!

    Then, are there any questions?

    My interpretation of your sql is:

        select * from users where Uid='' order by Uid desc

    reply
    0
  • 巴扎黑

    巴扎黑2017-05-16 13:02:47

    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

    reply
    0
  • Cancelreply