A
A
Anastasia2021-04-20 13:04:52
PHP
Anastasia, 2021-04-20 13:04:52

How to divide the text of a variable into several parts by cutting off on a certain word?

Hello. Telegram has a limit on the number of characters in a message: 4096. The situation that I am trying to prevent is unlikely, but possible. I need, in case of exceeding the message size limit, divide it into several pieces and send it. But there is a moment: my message looks like this:

$output = "<i>Запись №513</i> \nКакой-то текст на 50-150 символов\n".hex2bin('E380B0').hex2bin('E380B0').hex2bin('E380B0').hex2bin('E380B0').hex2bin('E380B0').hex2bin('E380B0').hex2bin('E380B0').hex2bin('E380B0').hex2bin('E380B0').hex2bin('E380B0').hex2bin('E380B0')."\n<i>Запись №514</i>\nA Какой-то текст на 50-150 символов";

hex2bin is a smiley (separating wave of the message.
607ea5d0866ad767255636.png
That is, if you just count 4000 characters, cut and send, you can run into the middle of some hex2bin.
The sending function itself looks like this:
function telega($method, $data) {
  $ch = curl_init("https://api.telegram.org/bot00000:00000000/$method");
  curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
  curl_setopt($ch, CURLOPT_CUSTOMREQUEST, "POST");
  curl_setopt($ch, CURLOPT_POSTFIELDS, $data);
  $curlRes1 = curl_exec($ch);
  echo $curlRes1;
}

$data = [
  "chat_id"=> $main_id,
  "text"=> $output,
  "parse_mode" => "html"
  ];

//Отправляем сообщение
telega("sendMessage", $data);


Can you please tell me how, in case of exceeding the length of the message, cut it into a word <i>Записьand send it already in 2 stages? That is, this is exactly the word with which the next infa begins.

Answer the question

In order to leave comments, you need to log in

1 answer(s)
P
PanDar, 2021-04-23
@PanDar

Well.... What is a string? it's almost an array, that is... we take strlen and check the length if it is greater than 4096, but insert a separator into the string at this place, for example "_-_" and divide the string by characters as many times as the array exceeds 4096

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question