dongxian7489 2014-04-23 22:06
浏览 73
已采纳

preg_replace_callback返回与输入相同

$post = preg_replace_callback("/\[smartquote]((?:(?!\[smartquote\]).)+?)\[\/smartquote\]/s", function($match) use ($pdo){
    if(isset($match[0])){
        $returnData = explode('_',$match[1]);
        if(empty($returnData[0]) || empty($returnData[1])){
            return 'Nothing found';
        } else {
            $getPost = $pdo->prepare("SELECT * FROM `posts` WHERE `tid`=:topic_id && `id`=:id");
                $getPost->bindValue(':topic_id',$returnData[0]);
                $getPost->bindValue(':id',$returnData[1]);
            $getPost->execute();
            if($getPost->rowCount() == 0){
                return '<div class="well"><h5>Quote:</h5><div style="font-size: 12px;">Quote Data</div></div>';
            } else {
                        return 'Something, something, darkside.';   
                }
        }
    }            
},$post);

The response I get on this everytime I try to use it, is the input. Can someone help me?

For example. If input $post = '[smartquote]44_12[/smartquote]'; - It will output the same result.

  • 写回答

1条回答 默认 最新

  • douzouchang7448 2014-04-25 13:40
    关注

    Well I tried your code and it seemed to work as expected, even though I do see that you're missing an escape in front of your first closing square brace. I thought that may throw things off, but to my surprise it worked.

    Here is the data that I used:

    CREATE TABLE IF NOT EXISTS `posts` (
      `id` int(9) unsigned NOT NULL AUTO_INCREMENT,
      `tid` int(9) unsigned NOT NULL DEFAULT '0',
      `post_title` varchar(100) NOT NULL,
      `post_data` text NOT NULL,
      PRIMARY KEY (`id`),
      KEY `topic_id` (`tid`)
    ) ENGINE=InnoDB AUTO_INCREMENT=13 DEFAULT CHARSET=latin1;
    
    INSERT INTO `posts` (`id`, `tid`, `post_title`, `post_data`) VALUES
        (12, 44, 'Nan took it', 'There once was a man from Nantucket who kept all his cash in a bucket.');
    

    I took your input string and modified your function a bit just to make sure that it was, in fact, connecting to and pulling data from the database. The code looks like this:

    $pdo = new PDO("mysql:host=localhost;dbname=so;","root","");
    
    $post = '[smartquote]44_12[/smartquote]';
    
    
    $post = preg_replace_callback("/\[smartquote]((?:(?!\[smartquote\]).)+?)\[\/smartquote\]/s", function($match) use ($pdo){
        if(isset($match[0])){
            $returnData = explode('_',$match[1]);
            if(empty($returnData[0]) || empty($returnData[1])){
                return 'Nothing found';
            } else {
                $getPost = $pdo->prepare("SELECT * FROM `posts` WHERE `tid`=:topic_id && `id`=:id");
                    $getPost->bindValue(':topic_id',$returnData[0]);
                    $getPost->bindValue(':id',$returnData[1]);
                $getPost->execute();
                if($getPost->rowCount() == 0){
                    return '<div class="well"><h5>Quote:</h5><div style="font-size: 12px;">Quote Data</div></div>';
                } else {
                            //return 'Something, something, darkside.';
    
                            $row = $getPost->fetchAll();
    
                            $post_title = $row[0]['post_title'];
                            $post_data = $row[0]['post_data'];
    
                            $return_data = "<b>".$post_title."</b><br><br>".$post_data;
    
                            return $return_data;
                    }
            }
        }            
    }, $post);
    
    print $post;
    

    This outputs the following:

    <b>Nan took it</b><br><br>There once was a man from Nantucket who kept all his cash in a bucket.
    

    Anyway, my suggestion would be to try and escape that first closing square brace and try it again. If the data is returning the same as what you put in, that likely means that it is unable to make a match in the first place. If that is in fact the case, then you may try rewriting your expression to see if you can get it to find a match another way.

    Depending on the data you are expecting, this may work and would store the topic id in $match[1] and the id in $match[2]:

    ~(?<=\[smartquote\])(?:(\d+)_(\d+))(?=\[/smartquote\])~s
    

    Then you should be able to omit this part altogether:

    if(isset($match[0])){
        $returnData = explode('_',$match[1]);
            if(empty($returnData[0]) || empty($returnData[1])){
            return 'Nothing found';
    } else {
    

    Since it won't run the function at all if $match[0] isn't set. Maybe you'd still like to check if $match[1] and $match[2] contain data, though.

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥15 如何让企业微信机器人实现消息汇总整合
  • ¥50 关于#ui#的问题:做yolov8的ui界面出现的问题
  • ¥15 如何用Python爬取各高校教师公开的教育和工作经历
  • ¥15 TLE9879QXA40 电机驱动
  • ¥20 对于工程问题的非线性数学模型进行线性化
  • ¥15 Mirare PLUS 进行密钥认证?(详解)
  • ¥15 物体双站RCS和其组成阵列后的双站RCS关系验证
  • ¥20 想用ollama做一个自己的AI数据库
  • ¥15 关于qualoth编辑及缝合服装领子的问题解决方案探寻
  • ¥15 请问怎么才能复现这样的图呀