tags:

views:

71

answers:

2

How do I take off a certain character in a string and put them all together in an array like:

"{2} in better that {1} when it comes to blah blah blah"

and the output would be:

array(0 => "2", 1 => "1");

I have used regular expression but it seems like it doesn't loop throughout the string or maybe I'm missing something?

Thanks

+5  A: 

Use preg_match_all instead of preg_match:

<?php
$str = "{2} in better that {1} when it comes to blah blah blah";
preg_match_all('/{\d+}/', $str, $matches);
print_r($matches[0]);
?>

Shows on my machine:

Array
(
    [0] => {2}
    [1] => {1}
)
catchmeifyoutry
+2  A: 
preg_match_all('/\{\d+\}/', $yourString, $matches);

var_dump($matches);
Jordan Ryan Moore