hippout
hippout

Reputation: 970

PHP String Parsing

I have string which contains something about "amount 3 val 6, amount 7 val 8" and so, what regular expression should I use to get array with corresponding amounts and values?

Upvotes: 2

Views: 234

Answers (3)

Jordan Messina
Jordan Messina

Reputation: 1531

amount\s*(\d*)\s*val\s*(\d*)

You can do the rest

Upvotes: 0

Matteo Riva
Matteo Riva

Reputation: 25060

$str = 'amount 3 val 6, amount 7 val 8';
preg_match_all('#amount\s+(\d+)\s+val\s+(\d+)#', $str, $matches);

All amounts in $matches[1], all vals in $matches[2].

Upvotes: 0

Alix Axel
Alix Axel

Reputation: 154513

$str = "amount 3 val 6, amount 7 val 8";
preg_match_all('~amount (\d+) val (\d+)~i', $str, $matches);

echo '<pre>';
print_r($matches);
echo '</pre>';

Upvotes: 1

Related Questions