2017-01-28 4 views
1

Ich versuche derzeit, einen Satz in ein Zeichen begrenzte Arrays zu teilen. Die Sätze werden unter Verwendung von explode in Wörter aufgeteilt und dann wird jedes Wort zu einem Array von Sätzen hinzugefügt, wenn die Länge des Strings des aktuellen Index kleiner als ie ist. 135. Aber ich habe gerade ein Problem damit, das Limit richtig zu machen, ich bin mir nicht sicher, was ich falsch mache. Jede Hilfe wird sehr geschätzt.Split-Satz in Zeichen begrenzte Arrays

<?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

Bitte erläutern Sie genau, was das Problem ist. Sind die Satzarrays länger als 135 Zeichen? – RiggsFolly

+0

@RiggsFolly ja die Länge der Zeichenfolge in den Arrays sind länger als die angegebene Länge – kye

+0

Also muss ein Satz 135 Zeichen ODER weniger rechts sein? – RiggsFolly

Antwort

2

Sie haben nur vergessen, die Größe des neuen Wort hinzuzufügen, die Sie vor der Entscheidung zu den bestehenden Satz hinzufügen würden es hinzuzufügen oder einen neuen Satz

starten Mods Siehe

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."); 

Ergebnis

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." 
}