Regex Search Help

For a line like:

a:2:{i:0;s:1:"1";i:1;s:1:"2";}

      

I want to find any integer inside quotes and create an array of all integers found in the string.

The end result should be an array like:

Array
(
    [0] => 1
    [1] => 2
)

      

I assume you are using preg_match (), but I have no experience with regex :(

+1


a source to share


2 answers


How about this:

 $str = 'a:2:{i:0;s:1:"1";i:1;s:1:"2";}';
 print_r(array_values(unserialize($str)));

      



Not a regex, same answer.

This works because the string you are using is a PHP serialized array. Using a regex would be the wrong way to do this.

+7


a source


The regular expression (in the program) will look like this:



$str = 'a:2:{i:0;s:1:"1";i:1;s:1:"2";}';
preg_match_all('/"(\d+)"/', $str, $matches);
print_r($matches[1]);

      

0


a source







All Articles