开发者

Preg_replace for price string (PHP)

Trying to conv开发者_如何学Pythonert a string with some price "2 200,00" (without quotes) to look like 2200.

$result = preg_replace("/([^0-9,]s)/iu","", $result);

Doesn't work. Could sombody help, pls?


Whats the s good for? And the brackets are also not needed. Try

$result = preg_replace("/[^0-9,]+/iu","", $result);

Probably better to watch only for whitspaces

$result = preg_replace("/\w+/iu","", $result);

+ means match one ore more, i.e. if there is more than one whitespace it would be matched at once.


Try with

$result = preg_replace("/[,\s+]/iu","", $result);


http://www.crainbandy.com/programming/function-to-remove-all-non-numeric-characters-in-php


You can do this with intval and str_replace instead of preg_replace.

// Results in 2200 (without decimals)
$result = intval(str_replace(' ', '', $result));


Regex is not required for this.

$number = '2 200,00';
echo str_replace(array(' ', ','), '', substr($number, 0, strrpos($number, ',')));

(If the number string always contains a comma)

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜