I have the following code, which I need to tweak, in order to get the desired echo.
<?php
$price = "A1,500.99B";
$pattern = '/([\d,]+\.)(\d+)(.*)$/'; // This is something that I need to change, in order to get the desired result
$formatted_1 = preg_replace($pattern, '$1', $price);
$formatted_2 = preg_replace($pattern, '$2', $price);
$formatted_3 = preg_replace($pattern, '$3', $price);
$formatted_4 = preg_replace($pattern, '$4', $price);
echo $formatted_1; // Should give A
echo $formatted_2; // Should give 1,500
echo $formatted_3; // Should give 99
echo $formatted_4; // Should give B
?>
I know that I should add another ( ) with something inside, within the $pattern, as well as tweak the above $pattern, but I have no idea what to do.
Thanks.
Any particular reason to use preg_replace if you just want a match?
This pattern will match your price:
If you then write this PHP:
You will have the four matches. Obviously you need to add your own exception handling.