2017-01-28 27 views
1

我正在嘗試將句子拆分爲字符受限陣列。如果當前索引的字符串長度小於ie,則使用explode將句子拆分爲單詞,然後將每個單詞添加到句子數組中。 135.但是,我目前有一個正確的限制問題,我不太確定我做錯了什麼。任何幫助將不勝感激。將句子拆分爲字符受限陣列

<?php 

function parseDefinition($def){ 

    $tweets = []; 
    $index = 0; 
    $wordsArr = explode(" ", $def); 
    $sentence = ""; 
    $length = 135; 
    for ($i = 0; $i < count($wordsArr); $i++){ 
     if (!isset($sentences[$index])){ 
      $sentences[$index] = $wordsArr[$i]; 
     }else{ 
      $sentenceLength = strlen($sentences[$index]); 
      if ($sentenceLength <= $length){ 
       $sentence = $sentences[$index] . " " . $wordsArr[$i]; 
       $sentences[$index] = $sentence; 
      }else{ 
       $index ++; 
       $sentence = $wordsArr[$i]; 
       $sentences[$index] = $sentence; 
      } 
     } 
    } 
    var_dump($sentences); 

} 

parseDefinition("Vikings follows the adventures of Ragnar Lothbrok, the greatest hero of his age. The series tells the sagas of Ragnar's band of Viking brothers and his family, as he rises to become King of the Viking tribes. As well as being a fearless warrior, Ragnar embodies the Norse traditions of devotion to the gods. Legend has it that he was a direct descendant of Odin, the god of war and warriors."); 


?> 
+0

請詳細解釋問題所在。是否句數最後超過135個字符 – RiggsFolly

+0

@RiggsFolly是數組中的字符串長度超過了指定的長度 – kye

+0

所以一個句子必須是135個字符還是不對? – RiggsFolly

回答

2

你只是忘決定添加或啓動一個新的句子前添加你要添加到現有的一句新字的大小

見MODS

function parseDefinition($def){ 

    $tweets = []; 
    $index = 0; 
    $wordsArr = explode(" ", $def); 
    $sentence = ""; 
    $length = 135; 
    for ($i = 0; $i < count($wordsArr); $i++){ 
     if (!isset($sentences[$index])){ 
      $sentences[$index] = $wordsArr[$i]; 
     }else{ 
      // Add the new words size to the calc before adding to sentence 
      // plus 1 for the space you are also going to add 
      if (strlen($sentences[$index]) + strlen($wordsArr[$i]) + 1 <= $length){ 
       $sentence = $sentences[$index] . " " . $wordsArr[$i]; 
       $sentences[$index] = $sentence; 
      }else{ 
       $index ++; 
       $sentence = $wordsArr[$i]; 
       $sentences[$index] = $sentence; 
      } 
     } 
    } 
    var_dump($sentences); 

} 

parseDefinition("Vikings follows the adventures of Ragnar Lothbrok, the greatest hero of his age. The series tells the sagas of Ragnar's band of Viking brothers and his family, as he rises to become King of the Viking tribes. As well as being a fearless warrior, Ragnar embodies the Norse traditions of devotion to the gods. Legend has it that he was a direct descendant of Odin, the god of war and warriors."); 

結果

array(3) { 
    [0] => 
    string(134) "Vikings follows the adventures of Ragnar Lothbrok, the greatest hero of his age. The series tells the sagas of Ragnar's band of Viking" 
    [1] => 
    string(130) "brothers and his family, as he rises to become King of the Viking tribes. As well as being a fearless warrior, Ragnar embodies the" 
    [2] => 
    string(125) "Norse traditions of devotion to the gods. Legend has it that he was a direct descendant of Odin, the god of war and warriors." 
}