2
SetColoursSizes('0', '[Select Colour]', '0', '[Select Size]', 0,585.00,500.00);

This is an example of the string I'd like to match in PHP, and then caputre the last two variables, in case, '585.00' and '500.00'. Note: these numbers should be flexible in that they could be anything from 0.50 to $1500.00.

Can anyone help with the PHP/RegEx for this?

Cheers, Michael

2
  • Just out of curiosity, is your PHP regexing other PHP? Commented Jul 5, 2011 at 2:34
  • No, it was scraping a page so it was regexing Javascript nested within a whole HTML page Commented Jul 6, 2011 at 1:47

3 Answers 3

2

This regex will work:

SetColoursSizes\s*\([^(]+,\s*([0-9.$]+)\s*,\s*([0-9.$]+)\s*\)
Sign up to request clarification or add additional context in comments.

Comments

1

The following code captures your final two number/decimal arguments in $matches[2] and $matches[3]:

<?php
  $matches = array();
  preg_match('/SetColoursSizes\(([^,]+,\s*){5}(\d+\.?\d*),\s*(\d+\.?\d*)/', "SetColoursSizes('0', '[Select Colour]', '0', '[Select Size]', 0,0.50,500.00);", $matches);
  print_r($matches);
?>

Comments

1
$matches = array();
preg_match('~([\d\.]+),\s*([\d\.]+)\);$~', $yourString, $matches);

$matches[1] and $matches[2] should have the two numbers.

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.