将句子分成字符限制数组

时间:2017-01-28 18:03:19

标签: php

我目前正试图将一个句子分成一个字符限制数组。通过使用explode将句子分成单词,然后如果当前索引的字符串长度小于ie,则将每个单词添加到句子数组中。 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.");


?> 

1 个答案:

答案 0 :(得分: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."
}