【问题标题】:Break up long string with PHP for outgoing SMS用 PHP 分解长字符串以发送短信
【发布时间】:2011-02-16 22:32:42
【问题描述】:

我的客户正在寻找一种将文本消息发送到我编写的 Twilio PHP 脚本的方法,然后将其转播给现场人员。这就是简单的部分,只需检查来电号码是否授权,从 MySQL 中提取人员详细信息,然后分发。

这是棘手的部分 - 使用它的人可能会啰嗦,他们的手机允许他们输入 160 多个字符。假设 Twilio 可以接收 >160 个字符(我知道它不能发送 >160 个字符),我需要将这个长消息(字符串)分解成 160 个字符以下的块。

这是我想出的脚本,它效果很好,但我希望它以一个完整的单词结束,而不是简单地分割后的下一个字符。有趣的插曲,当你忘记输入分割字符串的长度时,你会收到 171 条左右的一个字符的短信! :P

<?php
$string = "Ulysses, Ulysses - Soaring through all the galaxies. In search of Earth, flying in to the night. Ulysses, Ulysses - Fighting evil and tyranny, with all his power, and with all of his might. Ulysses - no-one else can do the things you do. Ulysses - like a bolt of thunder from the blue. Ulysses - always fighting all the evil forces bringing peace and justice to all.";
$arr = str_split($string, 155); //Factoring in the message count
$num = count($arr); //See how many chunks there are

$i = 1; //Set interval to 1
foreach ($arr as $value) {
    if ($num > 1) {
    echo "($i/$num) $value<br/>"; //(x/x) Message...
    $i++;
} else {
    echo $value; //Message...
}

}
?>

非常感谢

更正 抱歉,我的主要疏忽 - 我发布了开发脚本以使用字符串,而不是在“事件”之后发送实际 SMS 的实时脚本。我需要能够迭代这些块,以便在拆分后将每个块作为自己的 SMS 发送出去,就像上面那样......只是以一个完整的单词结尾。 SMS 将在 foreach 循环中发送。

【问题讨论】:

  • +1 有趣的旁白……该死的——希望你的手机可以“全部删除”,而不仅仅是单独删除
  • 确实如此,但由于那个 LOL,我用 Twilio 换了 3.40 美元的零钱
  • 难道他们没有办法在您的代码“上线”之前免费测试您的代码,这可能是他们的疏忽吗?
  • 我相信是这样,但由于问题只是如何将一个长字符串拆分成一个可以迭代的数组,所以我在操场上制作了一个脚本,而不是处理实时文件。但是,我没有复制和粘贴我的工作解决方案(没有以完整单词结尾),而是再次输入了代码 - 忘记了拆分的长度。墨菲定律;)
  • @Daniel,我们为所有新客户提供 30 美元来测试服务。

标签: php string twilio


【解决方案1】:

当您可以使用时,我不明白为什么所有过于复杂的答案:

$chunks = explode("||||",wordwrap($message,155,"||||",false));
$total = count($chunks);

foreach($chunks as $page => $chunk)
{
    $message = sprintf("(%d/%d) %s",$page+1,$total,$chunk);
}

这会产生类似的结果:

(1/3) Primis facilis apeirian vis ne。 Idque ignota est ei。 Ut sonet indoctum nam, ius ea illum fabellas。 Pri delicata percipitur ad, munere ponderum rationibus。

在线示例:http://codepad.org/DTOpbPIJ更新

【讨论】:

  • 这基本上是我已经拥有的 - 问题是以一个完整的单词结尾,而不是一个片段。
  • 这看起来可能很理想,|||| 背后的故事是什么?在explode()中?
  • 基本上 wordwrap 用一定数量的字符分隔字符串,这是你想要的,但它不会将它们分隔成一个数组,它主要用于段落保持在一个边距内,你通常会使用诸如“
    ”之类的想法,|||| 的原因是它的独特性,所以当我们通过它爆炸时,我们得到一个数组中的分割段,其中包含单词边界和 |||| 删除 -您通常会做一些谎言-A_secrEtSpLiT((- 以使其更加独特。
【解决方案2】:

尝试自动换行:http://www.php.net/manual/en/function.wordwrap.php

<?php

$words = "this is a long sentence that needs splitting";


foreach(explode("\n", wordwrap($words, 10, "\n")) as $s) {
        echo $s . "\n";
}

【讨论】:

    【解决方案3】:

    您可以先explode() 字符串,使用空格作为分隔符。获得数组后,开始循环遍历它并将单词逐个添加到字符串中。在将单词添加到字符串之前检查总字符串长度是否超过 160。如果是这样,开始一个新的字符串。你可以通过存储一个字符串数组来做到这一点。

    <?php
    $string = "Ulysses, Ulysses - Soaring through all the galaxies. In search of Earth, flying in to the night. Ulysses, Ulysses - Fighting evil and tyranny, with all his power, and with all of his might. Ulysses - no-one else can do the things you do. Ulysses - like a bolt of thunder from the blue. Ulysses - always fighting all the evil forces bringing peace and justice to all."
    
    $arr = explode(' ', $string); // explode by space
    $msgs = array(); // array of finished messages
    
    $i = 0; // position within messages array
    foreach($arr as $word)
    {
        if (strlen($msgs[$i] . $word) <= 155) // this may be 160, im not sure
        {
            $msgs[$i] .= $word;
        }
        else
        {
            $i++;
            $msgs[$i] = $word;
        }
    }
    ?>
    

    【讨论】:

    • 谢谢你,Scott。我实际上是在学习它,直到我看到 Robert 的代码,它更紧凑,更容易融入已经很庞大的脚本中(我会试着把它剪掉)一些,也是)。感谢您的帮助:)
    【解决方案4】:

    这个怎么样(注意:未经测试,不合时宜,但你明白了):

    $string = '<your long message>';
    $parts = array();
    while (strlen($string) > 155) {
        $part = substr($string, 0, 155);
        $lastSpace = strrpos($part, ' ');
    
        $parts[] = substr($string, 0, $lastSpace);
        $string = substr($string, $lastSpace);
    }
    $parts[] = $string;
    
    var_dump($parts);
    

    【讨论】:

      猜你喜欢
      • 2017-10-27
      • 2017-07-17
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2016-01-19
      • 1970-01-01
      相关资源
      最近更新 更多