使用 PHP 分解长字符串以发送短信

发布于 2024-10-17 10:36:28 字数 1267 浏览 2 评论 0原文

我的客户正在寻找一种方法,将文本消息发送到我编写的 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 将在 foreach 循环中发送。

My client is looking for a way to send a text message to the Twilio PHP script I programmed and then have it rebroadcast to personnel in the field. That's the easy part, simply check to see if the incoming number is authorized, pull the personnel details from MySQL, and distribute.

Here's the tricky part - the people using this may be long winded and their phones allow them to enter more than 160 characters. Assuming that Twilio can receive >160 characters (I know it cannot send >160), I need to break this long message (string) up into chunks that fall under 160 characters.

Here is the script that I came up with to do so, it works great but I would like for it to end on a complete word, rather than simply the next character after it is split. Funny side story, when you forget to enter the length at which to split the string, you receive 171 or so one character text messages! :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...
}

}
?>

Many thanks

CORRECTION
Sorry, major oversight on my part - I posted the development script to play with the strings rather than the live script that sends the actual SMS after "the incident." I need to be able to iterate over the chunks to send each out as its own SMS after it is split, as the above will do.. just ending on a complete word. The SMS would be sent in the foreach loop.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(4

乞讨 2024-10-24 10:36:28

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

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

I don't get why all the over complicated answers when you can just use:

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

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

This would produce something like:

(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.

Online Example: http://codepad.org/DTOpbPIJ Updated

樱花落人离去 2024-10-24 10:36:28

尝试自动换行: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";
}

Try wordwrap: 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";
}
寄与心 2024-10-24 10:36:28

您可以先 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;
    }
}
?>

you can explode() the string first, using a space as a delimiter. Once you have the array, start cycling through it and adding words to a string one by one. Check if the total string length would be over 160 before adding the word onto the string. If so, start a new string. You can do this by storing an array of strings.

<?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;
    }
}
?>
素食主义者 2024-10-24 10:36:28

怎么样(注:未经测试,我的想法,但你明白了):

$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);

How about this (Note: untested, off-the-top-of-my-head, but you get the idea):

$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);
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文