2015-10-23 32 views
2

我解析的文本文件看起来像这样:我怎样才能解析一个精确的字符串长度的字符串?

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数据库和检查的字符串长度cat5

$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后没有空格。那么是什么原因?

回答

1

您从$properties

得到$row您可以设置每个元素在$properties与其中的关键$type您使用$value

一个键值对数组后者来自

$value = $lineContArray[1];

并再次上涨

$lineContArray = explode("=", $line);

这很可能是$line以空格或换行符结尾。

所以我会修剪$value

$value = trim($lineContArray[1]);

因为你已经在做,当你在第三线

+0

完美,这解决了我的问题!非常感谢您 – Jarla

1

首先设定$prop,确保你不计数,包括换行符字符在字符串的末尾,你可以修剪字符以确保你没有这样做,但我建议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; 
} 
+0

嗨,非常感谢您的帮助!你会在我的案例中建议哪种方法? – Jarla

+1

第一个是最快的,因为它只经过一次字符串,最新的代码更小,我只是想展示几种方法,你可以选择更适合你的那个 – ogres

+0

非常感谢! – Jarla

相关问题