我解析的文本文件看起来像这样:我怎样才能解析一个精确的字符串长度的字符串?
2.cat=262FU
2.dog=FSK4A
票数是代码:
if (false !== ($pos = strpos($line, '='))) {
$prop=array();
$prop[trim(substr($line, 0, $pos))] = trim(substr($line, $pos + 1));
$lineContArray = explode("=", $line);
$identArray = explode(".", $lineContArray[0]);
$ident = $identArray[0];
$type = $identArray[1];
$value = $lineContArray[1];
$found = 0;
for ($i=0; $i<count($properties); $i++) {
if ($properties[$i]['number'] == $ident) {
$properties[$i][$type]= $value;
$found=1;
break;
}
}
if ($found == 0) {
if (!empty($type)) {
$properties[] = array('number' => $ident, $type => $value);
} else {
$properties[] = array($ident => $value);
}
}
}
我要插入的解析的内容到MySQL数据库和检查的字符串长度cat
是5
。
$sql = "INSERT INTO files (cat, dog) values(?,?) ";
$q = $pdo->prepare($sql);
foreach($properties as $row) {
var_dump ($row['cat']);
if (strlen($row['cat']) == 5){
$q->execute(array($row['cat'], $row['dog'];
}
} else {
echo "The length of cat is not 5";
}
}
我得到以下错误:
string(6) "262FU "
The length of cat is not 5
但在我的文本文件,有262FU
后没有空格。那么是什么原因?
完美,这解决了我的问题!非常感谢您 – Jarla