2

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?

1
  • Is there ever a case where you wouldn't have a value for something? For instance "amount val 5, amount 8 val 4, amount 6 val" Commented Jan 21, 2010 at 17:18

3 Answers 3

1
$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>';
Sign up to request clarification or add additional context in comments.

Comments

0
$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].

Comments

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

You can do the rest

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.