【发布时间】:2009-08-22 15:09:55
【问题描述】:
//matches[0] 保存一些 preg_match'ed 值
for($i=0;$i<count($matches[0]);$i++)
{
$price = $matches[0][$i]; //a**uto type casting to (float) returns 0 for $price. Tried it for values greater than 1 too.**
//echo gettype($price);
$price = $price + 0.01; **//Returns 0 + 0.01 instead of the right answer**
//**even after removing the above statement it does not compare**
if($price <= 1.50 && $price >= 1.00){ //because it should auto type cast to float, which it does no...
echo "here";
$price = $price+0.50;}
//strcmp always returns a '1' where 1 is surely not expected (not equal)
else if(strcmp($price,"1.50") > 0) && strcmp($price,"2.00") < 0 ){
echo "here 2";
$price = $price+0.50;}
}
这不是因为 $price 是一个常数,因为它属于一个循环吗?
我正常地尝试了同样的事情,没有循环,它正确地进行了类型转换。
我在这里错过了什么吗?
【问题讨论】:
-
生成
$matches的代码是什么?特别是正则表达式会很有帮助。目前我最好的猜测是它有一些空格,所以 $price =trim($matches[0][$i]);会有所帮助,但我怀疑这就是整个解决方案。 -
正则表达式是:-
preg_match_all("/(\d|\.)+/",$data,$matches);此外,修剪没有工作。好主意。 -
$data 是从文件中获取的
-
因为 foreach 给出了同样的问题......所以我想也许 for 循环会解决它.....
-
进一步,在代码的后面,在for循环中,我还使用了 $matches[0][$i] = str_replace(".","\.",$matches[0][ $i]); //用于转义“。” $data = preg_replace("/
".$matches[0][$i]."/",$price,$data,1);但它给了我这个错误“警告:preg_replace() [function.preg-replace]: Unknown modifier 'g' in”$matches[0][$i] has 1.80 as it's vlaue