首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >将句子分割成字符限制数组

将句子分割成字符限制数组
EN

Stack Overflow用户
提问于 2017-01-28 18:03:19
回答 1查看 35关注 0票数 1

我目前正在尝试将一个句子分割成一个字符限制数组。使用explode将句子拆分成单词,如果当前索引的字符串长度小于ie,则将每个单词添加到句子数组中。135.但是我现在对限制有一个问题,我不太确定我做错了什么。任何帮助都将不胜感激。

代码语言:javascript
复制
<?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.");


?> 
EN

回答 1

Stack Overflow用户

回答已采纳

发布于 2017-01-28 18:16:37

你只是忘了把你要添加到现有句子中的新单词的大小加进去,然后才决定添加它或开始一个新的句子。

见mods

代码语言:javascript
复制
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.");

结果

代码语言:javascript
复制
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."
}
票数 2
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/41913026

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档