【发布时间】:2012-08-16 00:45:14
【问题描述】:
我使用正则表达式从文档中获取值并将其存储在名为$distance 的变量中。那是一个字符串,但我必须将它放在数据库中表的 int 列中。
当然,通常我会去说
$distance=intval($distance);
但它不起作用!我真的不知道为什么。
这就是我所做的一切:
preg_match_all($regex,$content,$match);
$distance=$match[0][1];
$distance=intval($distance);
正则表达式是正确的,如果我回显 $distance,它是例如“0” - 但我需要它是 0 而不是“0”。使用 intval() 总是会以某种方式将其转换为空字符串。
编辑 1
正则表达式是这样的:
$regex='#<value>(.+?)</value>#'; // Please, I know I shouldn't use regex for parsing XML - but that is not the problem right now
然后我继续
preg_match_all($regex,$content,$match);
$distance=$match[0][1];
$distance=intval($distance);
【问题讨论】:
-
在解析之前转储你的距离变量,如果你不显示你正在处理的字符串,很难看出什么问题......还有正则表达式
-
我可能是错的,和/或遗漏了一些东西,但我认为“0”的字符串值将使其进入数据库中的 int 列而无需任何转换。
-
你试过像
$distance = (int) $distance;这样的简单转换吗? -
是的,我也尝试过 (int) - 请参阅上面的正则表达式。
-
距离回显之前你尝试转换它吗?试试看它的长度?也许有空白?
标签: php int type-conversion