我正在解析一个如下所示的文本文件:
2.cat=262FU
2.dog=FSK4A
hier是代码:
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
之后没有空格。那是什么原因?
答案 0 :(得分:1)
您从if (val1 < val2 && val1 < val3)
{
cout << val1 << " ";
if (val2 < val3)
{
cout << val2 << " ";
cout << val3 << " ";
}
else
{
cout << val3 << " ";
cout << val2 << " ";
}
}
$row
您可以使用键值对数组在$properties
中设置每个元素,其中您使用的键$properties
$type
后者来自
$value
再次上升
$value = $lineContArray[1];
$lineContArray = explode("=", $line);
可能以空格或换行符结尾。
所以我会修剪$line
$value
正如您在第三行设置$value = trim($lineContArray[1]);
时已经做的那样
答案 1 :(得分:1)
首先,确保你没有计算并包括字符串末尾的换行符,你可以修剪字符以确保你没有这样做,但我建议3种不同的方法,
$fileContent = file_get_contents('file');
$contentLength = strlen($fileContent);
// Direct String parsing approach
function parseString1($string) {
$result = Array();
$tempId = '';
$tempType = '';
$tempValue = '';
$step = 0;
$stringLength = strlen($string);
for($i = 0;$i < $stringLength;$i++) {
$char = $string[$i];
if($step == 0) {
if($char != '.') {
$tempId .= $char;
} else {
$step++;
}
} else if($step == 1) {
if($char != '=') {
$tempType .= $char;
} else {
$step++;
}
} else if($step == 2) {
if(ctype_space($char)) {
$result[] = Array( 'id' = $tempId, 'type' = $tempType, 'value' = $tempValue );
$step = 0;
$tempId = $tempType = $tempValue = '';
} else {
$tempValue .= $char;
}
}
}
if(!empty($tempId) && !empty($tempType) && !empty($tempValue)) {
$result[] = Array( 'id' = $tempId, 'type' = $tempType, 'value' = $tempValue );
}
return $result;
}
// Split string approach
function parseString2($string) {
$result = Array();
$lines = split("\n", $string);
foreach($lines as $line) {
$parts = explode('=', $line);
$value = trim($parts[1]);
$parts = explode('.', $parts[0]);
$id = $parts[0];
$type = $parts[1];
$result[] = Array( 'id' = $id, 'type' = $type, 'value' = $value );
}
return $result;
}
// Regex whole string approach
function parseString3($string) {
$result = Array();
$pattern = '/(\d+).([^=]+)=(.*)\b/';
$matchesCount = preg_match_all($pattern, $string, $matches);
for($i = 0;$i < $matchesCount;$i++) {
$result[] = Array( 'id' = $matches[1][$i], 'type' = $matches[2][$i], 'value' = $matches[3][$i] );
}
return $result;
}