2015-10-23 82 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

相關問題