我有一个算法,给出一个带小数点的数字(I.E.“.57”)。我想要做的只是得到没有小数的“57”。PHP从数字字符串中删除小数点
我已经使用eregi_replace和str_replace,都没有工作!
$one = ".57";
$two = eregi_replace(".", "", $one);
print $two;
我有一个算法,给出一个带小数点的数字(I.E.“.57”)。我想要做的只是得到没有小数的“57”。PHP从数字字符串中删除小数点
我已经使用eregi_replace和str_replace,都没有工作!
$one = ".57";
$two = eregi_replace(".", "", $one);
print $two;
$one = '.57';
$two = str_replace('.', '', $one);
echo $two;
这是行得通的。 100%测试。顺便说一句,所有ereg(i)_ *功能都折旧了。如果您需要正则表达式,请使用preg_ *。
$number = ltrim($number, '.');
,消除所有小圆点。
你说你已经使用str_replace函数没有任何的运气尝试过,但下面的代码工作完美:
<?php
$one = '.57';
$two = str_replace('.', '', $one);
echo $two;
?>
这将返回一个整数。
$two = (integer) trim('.', $one);
Method Result Command
x100 57 ((float)$one * 100))
pow/strlen 57 ((float)$one * pow(10,(strlen($one)-1))))
substr 57 substr($one,1))
trim 57 ltrim($one,'.'))
str_replace 57 str_replace('.','',$one))
只是shwoing得到同样的结果
我已经使用eregi_replace和str_replace函数和既不工作的一些其他的方法!
嗯... ereg_replace()
将无法正常工作的原因,而是使用常规expresions和.
(点)字符有特殊的意义:一切(所以你更换每一个字符为“”(空字符串))。
但是在这种情况下绝对正常工作。
这里有一个现场测试:http://ideone.com/xKG7s
SUBSTR($之一,1):d – Paul 2011-01-19 18:30:12
对不起,我们需要对输入和输出更精确的规格...的 – Paul 2011-01-19 18:30:38
可能重复[如何删除的第一个字符php](http://stackoverflow.com/questions/4638569/how-to-remove-the-first-character-in-php) – Gordon 2011-01-19 18:31:38