Sudipta Bhattacharyya Sudipta Bhattacharyya - 3 months ago 35
MySQL Question

extract encoded data from msql and decode in php

I am using a wordpress quiz plugin and I want to extract the questions and answers straight from the database table and use it to create a pdf document. The question is stored as a simple string which is easy to extract but the answers(multiple choice answers) are stored in the database column like this:

a:4:{i:0;O:27:"WpProQuiz_Model_AnswerTypes":7:{s:10:"*_answer";s:7:"Option1";s:8:"*_html";b:0;s:10:"*_points";i:1;s:11:"*_correct";b:1;s:14:"*_sortString";s:0:"";s:18:"*_sortStringHtml";b:0;s:10:"*_mapper";N;}i:1;O:27:"WpProQuiz_Model_AnswerTypes":7:{s:10:"*_answer";s:7:"Option2";s:8:"*_html";b:0;s:10:"*_points";i:1;s:11:"*_correct";b:0;s:14:"*_sortString";s:0:"";s:18:"*_sortStringHtml";b:0;s:10:"*_mapper";N;}i:2;O:27:"WpProQuiz_Model_AnswerTypes":7:{s:10:"*_answer";s:7:"Option3";s:8:"*_html";b:0;s:10:"*_points";i:1;s:11:"*_correct";b:0;s:14:"*_sortString";s:0:"";s:18:"*_sortStringHtml";b:0;s:10:"*_mapper";N;}i:3;O:27:"WpProQuiz_Model_AnswerTypes":7:{s:10:"*_answer";s:7:"Option4";s:8:"*_html";b:0;s:10:"*_points";i:1;s:11:"*_correct";b:0;s:14:"*_sortString";s:0:"";s:18:"*_sortStringHtml";b:0;s:10:"*_mapper";N;}}


Here the four options are 'Option1', 'Option2', 'Option3' and 'Option4'. How do I extract them as the four strings, or as a string array?

EDIT------------------------------------

As suggested I used the unserialize function as follows:

$ser ='a:4:{i:0;O:27:"WpProQuiz_Model_AnswerTypes":7:{s:10:"*_answer";s:7:"Option1";s:8:"*_html";b:0;s:10:"*_points";i:1;s:11:"*_correct";b:1;s:14:"*_sortString";s:0:"";s:18:"*_sortStringHtml";b:0;s:10:"*_mapper";N;}i:1;O:27:"WpProQuiz_Model_AnswerTypes":7:{s:10:"*_answer";s:7:"Option2";s:8:"*_html";b:0;s:10:"*_points";i:1;s:11:"*_correct";b:0;s:14:"*_sortString";s:0:"";s:18:"*_sortStringHtml";b:0;s:10:"*_mapper";N;}i:2;O:27:"WpProQuiz_Model_AnswerTypes":7:{s:10:"*_answer";s:7:"Option3";s:8:"*_html";b:0;s:10:"*_points";i:1;s:11:"*_correct";b:0;s:14:"*_sortString";s:0:"";s:18:"*_sortStringHtml";b:0;s:10:"*_mapper";N;}i:3;O:27:"WpProQuiz_Model_AnswerTypes":7:{s:10:"*_answer";s:7:"Option4";s:8:"*_html";b:0;s:10:"*_points";i:1;s:11:"*_correct";b:0;s:14:"*_sortString";s:0:"";s:18:"*_sortStringHtml";b:0;s:10:"*_mapper";N;}}';

$un = unserialize($ser);
if(!is_array($un)){
echo $un;
}else{
foreach($un as $value){
echo $value."\n";
}
}


I got the following error:

PHP Notice: unserialize(): Error at offset 63 of 822 bytes in <path> on line 5


How do I tackle this?

Answer

There is an error in the serialized string. Apparently all property names that start with *_ have a string length specified for them (e.g. s:10:) that counts 2 characters too many. This makes the unserialize function fail.

Here is code that deals specifically with this problem, and makes such keys 2 characters longer before applying the unserialize function:

// Fix problem with string lengths, specific to your case:
$ser = str_replace('"*_', '"_00_', $ser); // add two characters, avoid "*"
// Now you can unserialize without error:
$un = unserialize($ser);
// Avoid warnings about incomplete objects: turn objects to standard objects:
$un = json_decode(json_encode($un));
// Output the options
foreach($un as $obj){
    echo $obj->_00_answer."\n";
}
Comments