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.

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

报告相同问题?

悬赏问题

  • ¥30 这是哪个作者做的宝宝起名网站
  • ¥60 版本过低apk如何修改可以兼容新的安卓系统
  • ¥25 由IPR导致的DRIVER_POWER_STATE_FAILURE蓝屏
  • ¥50 有数据,怎么建立模型求影响全要素生产率的因素
  • ¥50 有数据,怎么用matlab求全要素生产率
  • ¥15 TI的insta-spin例程
  • ¥15 完成下列问题完成下列问题
  • ¥15 C#算法问题, 不知道怎么处理这个数据的转换
  • ¥15 YoloV5 第三方库的版本对照问题
  • ¥15 请完成下列相关问题!