Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

explode the price sign & the number

Tags:

regex

php

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

like image 404
hkguile Avatar asked Mar 05 '13 07:03

hkguile


2 Answers

$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

like image 131
cryptic ツ Avatar answered Sep 20 '22 12:09

cryptic ツ


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;
like image 43
Nirmal Ram Avatar answered Sep 23 '22 12:09

Nirmal Ram