I have an array of strings. Something like this
$preorder[] = "
iPhone 7/7 plusn
n
7 32Gb Jet B 1778 AA/A - 1 550n
7 32Gb Jet B 1778 HN/A - 50n
7 32Gb Jet B 1778 - 1 500n
7 32Gb Jet B 1778 LE/A - 1 550n
7 32Gb Jet B 1778 AH/A - 1 600n
7 32Gb Jet B 1778 VC/A - 1 550n
7 32Gb Jet B 1778 MY/A - 1 550n
7 32Gb Jet B 1778 2B/A - 1 550n
n
7 128Gb Black 1778 2B/A - 1 550n
7 128Gb Black 1778 VC/A - 1 550n
n
7 256Gb Black 1778 2B/A - 1 550n
n
7 128Gb Jet B 1778 VC/A - 1 550n
n
7 256Gb Black 1778 AH/A - 1 600n
7 256Gb Black 1778 AA/A - 1 550n
7 256Gb Black 1778 HN/A - 50n
7 256Gb Red 1778 MY/A - 1 550n
n
7 Plus 32Gb Black 1784 2B/A - 1 800n
n
7 256Gb Jet B 1778 MY/A - 4 950n
7 256Gb Jet Black 1778 - 1 500n
7 256Gb Jet B 1778 AH/A - 1 600n
7 256Gb Jet B 1778 HN/A - 50n
n
7 Plus 32Gb Silver 1784 2B/A - 5 200n
7 Plus 32Gb Black 1784 LE/A - 1 700n
7 Plus 32Gb Black 1784 HN/A - 50n
7 Plus 32Gb Black 1784 - 1 700n
7 Plus 32Gb Gold 1784 HN/A - 50n
7 Plus 32Gb Gold 1784 LE/A - 1 700n
7 Plus 32Gb Gold 1784 MY/A - 1 800n
7 Plus 32Gb Rose 1784 MY/A - 5 200n
n
7 Plus 128Gb Black 1784 2B/A - 5 200n
";
This is a list of some products and I will send it to telegram. But telegram has limit on message length. So if my message have more than 4000 chars I need to split it to 2 messages.
But it’s bad solution to split this string by empty line. So i have no idea how can I make this string looks like
$msg[] = "
iPhone 7/7 plusn
n
7 32Gb Jet B 1778 AA/A - 1 550n
7 32Gb Jet B 1778 HN/A - 50n
7 32Gb Jet B 1778 - 1 500n
7 32Gb Jet B 1778 LE/A - 1 550n
7 32Gb Jet B 1778 AH/A - 1 600n
7 32Gb Jet B 1778 VC/A - 1 550n
7 32Gb Jet B 1778 MY/A - 1 550n
7 32Gb Jet B 1778 2B/A - 1 550n
n
7 128Gb Black 1778 2B/A - 1 550n
7 128Gb Black 1778 VC/A - 1 550n";
$msg[]="
n
7 256Gb Black 1778 2B/A - 1 550n
n
7 128Gb Jet B 1778 VC/A - 1 550n
n
7 256Gb Black 1778 AH/A - 1 600n
7 256Gb Black 1778 AA/A - 1 550n
7 256Gb Black 1778 HN/A - 50n
7 256Gb Red 1778 MY/A - 1 550n";
$msg[] = "
n
7 Plus 32Gb Black 1784 2B/A - 1 800n
n
7 256Gb Jet B 1778 MY/A - 4 950n
7 256Gb Jet Black 1778 - 1 500n
7 256Gb Jet B 1778 AH/A - 1 600n
7 256Gb Jet B 1778 HN/A - 50n
n
7 Plus 32Gb Silver 1784 2B/A - 5 200n
7 Plus 32Gb Black 1784 LE/A - 1 700n";
$msg[] = "
7 Plus 32Gb Black 1784 HN/A - 50n
7 Plus 32Gb Black 1784 - 1 700n
7 Plus 32Gb Gold 1784 HN/A - 50n
7 Plus 32Gb Gold 1784 LE/A - 1 700n
7 Plus 32Gb Gold 1784 MY/A - 1 800n
7 Plus 32Gb Rose 1784 MY/A - 5 200n
n
7 Plus 128Gb Black 1784 2B/A - 5 200n
";
So I want to split string by parts and make an array of them and then just send it to telegram. My string may have 20000 chars length. So I need dynamic way to split it.
Any suggestions? Thx!
2
Answers
I solved my problem.
Thank you @sietse85 for array_chunk.
You can loop the exploded array of new line and count the number of characters in each line.
Sum it in a msgcount and keep adding to the string as long as you are below 4000 characters, once you go above 4000 split it and create a new array item.
Once all looping is done, you still have on message in the str, add that too and your done.
I added the count in the array also.
https://3v4l.org/JLPoU