根据字符数解析 PHP 字符串

发布于 2024-08-02 21:44:12 字数 332 浏览 13 评论 0原文

我开始编写一个小脚本,它接受一个字符串,计算字符数,然后根据字符数,拆分/分解字符串并一次发送/发送 110 个字符。

使用正确的逻辑/PHP 是什么:

1) Count the number of characters in the string
2) Preface each message with (1/3) (2/3) (3/3), etc...
3) And only send 110 characters at a time.

我知道我可能必须使用 strlen 来计算字符,以及某种类型的循环来循环,但我不太确定如何去做。

谢谢!

I'm starting to work on a small script that takes a string, counts the number of characters, then, based on the number of characters, splits/breaks the string apart and sends/emails 110 characters at a time.

What would be the proper logic/PHP to use to:

1) Count the number of characters in the string
2) Preface each message with (1/3) (2/3) (3/3), etc...
3) And only send 110 characters at a time.

I know I'd probably have to use strlen to count the characters, and some type of loop to loop through, but I'm not quite sure how to go about it.

Thanks!

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

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

发布评论

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

评论(3

时光无声 2024-08-09 21:44:12

如果您不关心在何处断开字符串,则可以使用 str_split

否则,如果您担心这一点(并且只想在空格上拆分),您可以执行以下操作:

// $str is the string you want to chop up.
$split = preg_split('/(.{0,110})\s/',
                    $str,
                    0,
                    PREG_SPLIT_DELIM_CAPTURE | PREG_SPLIT_NO_EMPTY);

使用此数组,您可以简单地执行以下操作:

$count = count($split);
foreach ($split as $key => $message) {
    $part = sprintf("(%d/%d) %s", $key+1, $count, $message);
    // $part is now one of your messages;
    // do what you wish with it here.
}

You could use str_split, if you're not concerned with where you break the strings.

Else, if you are concerned with this (and want to, say, split only on a whitespace), you could do something like:

// $str is the string you want to chop up.
$split = preg_split('/(.{0,110})\s/',
                    $str,
                    0,
                    PREG_SPLIT_DELIM_CAPTURE | PREG_SPLIT_NO_EMPTY);

With this array you could then simply do:

$count = count($split);
foreach ($split as $key => $message) {
    $part = sprintf("(%d/%d) %s", $key+1, $count, $message);
    // $part is now one of your messages;
    // do what you wish with it here.
}
尴尬癌患者 2024-08-09 21:44:12

使用 str_split() 并迭代结果数组。

use str_split() and iterate over the resulting array.

第几種人 2024-08-09 21:44:12

从我的想法来看,应该按原样工作,但并非必须如此。不过逻辑还是可以的。

foreach ($messages as $msg) {

  $len = strlen($msg);

  if ($len > 110) {
    $parts = ceil($len / 100);
    for ($i = 1; $i <= $parts; $i++) {
      $part = $i . '/' . $parts . ' ' . substr($msg, 0, 110);
      $msg = substr($msg, 109);
      your_sending_func($part);
    }

  } else {
    your_sending_func($msg);
  }

}

From the top of my head, should work as is, but doesn't have to. Logic is ok though.

foreach ($messages as $msg) {

  $len = strlen($msg);

  if ($len > 110) {
    $parts = ceil($len / 100);
    for ($i = 1; $i <= $parts; $i++) {
      $part = $i . '/' . $parts . ' ' . substr($msg, 0, 110);
      $msg = substr($msg, 109);
      your_sending_func($part);
    }

  } else {
    your_sending_func($msg);
  }

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