how can i explode the $123
or USD123
or CAD$123
or 元123
dynamically into
array [0] = "$"
array [1] = 123
?
$keywords = preg_split("/[^0-9]/", "$123");
The above is my testing code, but i can't get $
in array[0]
, any idea?
thanks
$string = '$123';
$string = preg_match('/^(\D+)(\d+)$/', $string, $match);
echo $match[1]; // $
echo $match[2]; // 123
Breakdown:
^(\D+)
match 1 or more non digit at beggining of string
(\d+)$
match 1 or more digits until end of string
Try this it would work for all.
$str = 'CAD$123';
$num = filter_var($str, FILTER_SANITIZE_NUMBER_INT);
$curr = explode($num,$str);
echo 'Your currency is '.$curr[0].' and number is '.$num;
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With