-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?

  • 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 '14 at 10:25

3 Answers3

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

Soorajlal K G
  • 778
  • 9
  • 20
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.

LS11
  • 261
  • 1
  • 6
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
rgunning
  • 568
  • 2
  • 16
  • 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 – user3805018 Jan 19 '15 at 11:03