6

我的客户正在寻找一种方法来向我编写的 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 循环中发送。

4

4 回答 4

11

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

$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 已 更新

于 2011-02-16T13:57:43.463 回答
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";
}
于 2011-02-16T14:14:26.587 回答
1

您可以先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;
    }
}
?>
于 2011-02-16T13:41:04.007 回答
0

这个怎么样(注意:未经测试,我的头,但你明白了):

$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);
于 2011-02-16T13:41:28.360 回答