使用ereg_replace$string = "$100,000";$int = ereg_replace("[^0-9]", "", $string); echo $int;
输出百万function toInt($str){
return (int)preg_replace("/\..+$/i", "", preg_replace("/[^0-9\.]/i", "", $str));}
更新<?php
$string = array("$1,000,000.00","$1 000 000.00","1,000 000.00","$123","$123 456 789","0.15¢");foreach($string as $s) {
echo $s . " = " . toInt($s) . "\n"; }function toInt($str){
return preg_replace("/([^0-9\\.])/i", "", $str);}?>
输出$1,000,000.00 = 1000000.00$1 000 000.00 = 1000000.001,000 000.00 = 1000000.00$123 = 123$123 456 789 = 1234567890.15¢ = 0.15
如果将其强制转换为整数<?php
$string = array("$1,000,000.00","$1 000 000.00","1,000 000.00","$123","$123 456 789","0.15¢");foreach($string as $s) {
echo $s . " = " . _toInt($s) . "\n"; }function _toInt($str){
return (int)preg_replace("/([^0-9\\.])/i", "", $str);}?>
输出$1,000,000.00 = 1000000$1 000 000.00 = 10000001,000 000.00 = 1000000$123 = 123$123 456 789 = 1234567890.15¢ = 0
所以你有它。单行,一次替换。你很好。