Php 将句子拆分为字符限制数组

Php 将句子拆分为字符限制数组,php,Php,我目前正试图将一个句子拆分为一个字符有限的数组。使用explode将句子拆分为单词,然后如果当前索引的字符串长度小于ie.135,则每个单词将添加到句子数组中。但我目前在正确设置限制方面遇到了问题,我不太确定我做错了什么。任何帮助都将不胜感激 <?php function parseDefinition($def){ $tweets = []; $index = 0; $wordsArr = explode(" ", $def); $sentence =

我目前正试图将一个句子拆分为一个字符有限的数组。使用
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.");


?> 

在决定添加新词或开始新句子之前,您只是忘记了在现有句子中添加新词的大小

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

请确切解释问题所在。是不是句子数组的长度超过了135chars@RiggsFolly是,数组中字符串的长度大于指定长度,因此句子必须小于等于135个字符,对吗?@RiggsFolly是的,这是因为在连接字符串之前检查字符串长度,您的字符串长度直到单词“viking乐队”为134,因此当您检查长度并发现长度为134时,您的条件正确,然后连接“brothers”,检查@RiggsFolly答案
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."
}