I have these types of possible content in the strings I’m breaking up:
$string = "$100 USD per year"
$string = "$100USD per year"
$string = "100 USD per year"
$string = "100EUR per year"
$string = "€100EUR per year"
$string = "€100 EUR per year"
This is the regular expression I am using (and it works in Expresso):
$pattern = "/\$?(\d+\.\d+)(\w{0,3})\s(.*)/i";
Basically, I want the end result to be an array like the following:
$arr[0] = "$100" // the "$" optional, also number might be integer or decimal
$arr[1] = "USD";
$arr[2] = "per year";
This is the sample PHP code I’m running but to no avail:
<?php
$content = "$99.37CAD per year";
$pattern = "/\$?(\d+\.\d+)(\w{0,3})\s(.*)/i";
$myArr = preg_split($pattern, $content);
echo "<pre>";
var_dump($myArr);
echo "</pre>";
?>
And the output is:
bool(false)
Can someone please point me in the right direction?
The problem is that the
SPACEbetween the price and the EUR/CAD/USD, is not optional. So you just have to add a*to it, like\s*. With that you will tell RegEx that that space is optional so it can be or not.Another thing is that you must match, not split. With the decimal digits optional.
DEMO Regex:
[$€]?(\d+(?:\.\d+)?)\s*(\w{0,3})\s*(.*)