我解析的文本文件看起來像這樣:我怎樣才能解析一個精確的字符串長度的字符串?
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