0
$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.

7
  • Perhaps you are mistaken? ideone.com/UB0w1Q Commented Apr 23, 2014 at 22:21
  • Is it because im using PDO to query a db? Commented Apr 23, 2014 at 22:24
  • I don't know, but I can help you to debug it. If you are running PHP on your own machine you should try a tool called xdebug which lets you trace through the execution and check the value of any variables, but for now, try just printing some values. Try adding var_dump($match); to the top of your function. Commented Apr 23, 2014 at 22:28
  • I did a var_dump of $match, $returnData, and $getPost and all the variables are present. Commented Apr 23, 2014 at 22:34
  • Do they contain the expected values? Is $returnData an array containing [44,12]? Commented Apr 23, 2014 at 22:38

1 Answer 1

1

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.

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.