-1

How to do I get all strings enclosed in a pair of double-quotes by using php, I want to get them in the following string,

$str = 'a:2:{i:1;a:4:{i:1;s:4:"2000";i:2;s:8:"10th STD";i:3;s:9:"Full Time";i:4;s:24:"State Board of Education";}i:2;a:4:{i:1;s:4:"2003";i:2;s:8:"12th STD";i:3;s:9:"Full Time";i:4;s:24:"State Board of Education";}}';

I want output as follows

2000
10th STD
Full Time
State Board of Education

I tried the following code but output comes only 2000

<?php

$str = 'a:2:{i:1;a:4:{i:1;s:4:"2000";i:2;s:8:"10th STD";i:3;s:9:"Full Time";i:4;s:24:"State Board of Education";}i:2;a:4:{i:1;s:4:"2003";i:2;s:8:"12th STD";i:3;s:9:"Full Time";i:4;s:24:"State Board of Education";}}'

if (preg_match('/"([^"]+)"/', $str, $m)) 
{
print $m[1];   
} 
else 
{

}
<?

Please suggest me how to do it, which function should I used to get my output?

1
  • 3
    That looks like a PHP serialized array. Use unserialize() to unserialize it, then treat it as array. Don't even start with using regular expressions here.
    – deceze
    Oct 28, 2014 at 10:25

3 Answers 3

8

Hope this will help u.

<?php
        $str = 'a:2:{i:1;a:4:{i:1;s:4:"2000";i:2;s:8:"10th STD";i:3;s:9:"Full Time";i:4;s:24:"State Board of Education";}i:2;a:4:{i:1;s:4:"2003";i:2;s:8:"12th STD";i:3;s:9:"Full Time";i:4;s:24:"State Board of Education";}}';

        $str_array = unserialize($str);  
        foreach($str_array as $values) {
            foreach($values as $value) {
                echo $value . '<br/>';
            }
            echo '<br/>';
        }
?>

will return:

2000
10th STD
Full Time
State Board of Education

2003
12th STD
Full Time
State Board of Education

1
  • i need to print the above output like <tr> <td>2000</td> <td>10th STD</td><td>Full Time</td><td>State Board</td> <tr> How to do i split like row value? Nov 20, 2014 at 11:48
1

It looks like a bog standard serialized array, so just apply unserialize() on the string, then parse through the values and apply is_string on them, and you'll achieve the result in a clean way.

1

As said above, unserialize() then convert to string

<?php
    $str = 'a:2:{i:1;a:4:{i:1;s:4:"2000";i:2;s:8:"10th STD";i:3;s:9:"Full Time";i:4;s:24:"State Board of Education";}i:2;a:4:{i:1;s:4:"2003";i:2;s:8:"12th STD";i:3;s:9:"Full Time";i:4;s:24:"State Board of Education";}}';
    $str=unserialize($str);
    $str2=implode("<br>",$str[1]);
    print $str2."\n";
?>

Gives

2000<br>10th STD<br>Full Time<br>State Board of Education
1
  • I want to display like Year is column and column value should be 2000 and Class is Column and Class value STD like this i want to display. can u give any idea for this Jan 19, 2015 at 11:03

Not the answer you're looking for? Browse other questions tagged or ask your own question.