tags:

views:

63

answers:

2
+1  Q: 

Regex Search Help

Given a string such as:

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

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

End result should be an array like:

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

I'm guessing you use preg_match() but I have no experience with regular expressions :(

+6  A: 

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 have is a serialized PHP array. Using a regex would be the wrong way to do this.

Peter Stuifzand
A: 

The regex (in a program) would 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]);
Peter Stuifzand