首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >用PHP拆分长字符串用于发送SMS

用PHP拆分长字符串用于发送SMS
EN

Stack Overflow用户
提问于 2011-02-16 13:32:02
回答 4查看 3.6K关注 0票数 6

我的客户正在寻找一种方式来发送一条短信给我编程的Twilio脚本,然后让它重新广播给现场人员。这是很简单的部分,只需检查输入号码是否被授权,从MySQL中提取人员详细信息,并分发。

这里有一个棘手的部分--使用这款手机的人可能会长时间呼吸,他们的手机允许他们输入超过160个字符。假设Twilio可以接收>160个字符(我知道它不能发送>160个字符),我需要将这条长消息(字符串)分解成块,这些块位于160个字符以下。

下面是我想出的脚本,它工作得很好,但我希望它以完整的单词结尾,而不是在拆分后的下一个字符。有趣的是,当您忘记输入拆分字符串的长度时,您将收到171条左右的字符短信!:P

代码语言:javascript
复制
<?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发送出去,就像上面提到的那样。只是以一个完整的词结束。短信将在前端循环中发送。

EN

回答 4

Stack Overflow用户

回答已采纳

发布于 2011-02-16 13:57:43

我不明白为什么你可以用:

代码语言:javascript
复制
$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 apeirian vis ne.不光彩的。Ut sonet indoctum nam,ius ea illum fabellas.[医].

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

票数 11
EN

Stack Overflow用户

发布于 2011-02-16 14:14:26

尝试单词包装:http://www.php.net/manual/en/function.wordwrap.php

代码语言:javascript
复制
<?php

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


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

Stack Overflow用户

发布于 2011-02-16 13:41:04

您可以首先使用空格作为分隔符,爆炸()字符串。一旦有了数组,就开始循环遍历它,然后一个一个地将单词添加到字符串中。在将单词添加到字符串之前,检查字符串的总长度是否超过160。如果是,启动一个新字符串。您可以通过存储字符串数组来做到这一点。

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

https://stackoverflow.com/questions/5017056

复制
相关文章

相似问题

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