массивы — разбить по символам абзаца на 3 равные части в переполнении стека

Если у меня есть параграф, он мне нужен, чтобы разделить его на 3 равные части (по символам).

Это моя строка

$string="this is my long text this is my long text this
is my long text this is my long text
this is my long text this is my
long text this is my long text this is my long text
this is my long text  this is my long text";

Что мне нужно это:

  • Равные символы в 3 частях или
  • 35% в части 1, 35% в части 2 и 30% в части 3 (3 части — по символам, а не по слову или строке)

Есть эксперты?

0

Решение

Проверьте это также

$string="this is my long text this is my long text this
is my long text this is my long text
this is my long text this is my
long text this is my long text this is my long text
this is my long text  this is my long text";

$strlen=strlen($string);

$first= intval($strlen * (35/100));
$second=intval($strlen * (35/100));
$third=intval($strlen * 30/100);$first_part=substr($string,0,$first);
$second_part=substr($string,$first,$second);
$third_part=substr($string,($first+$second));
1

Другие решения

Решение с использованием mb_strlen() а также mb_substr() функции:

$chunk_size = round(mb_strlen($string) * 0.35);
$parts = [];
foreach ([0, $chunk_size, $chunk_size * 2] as $pos) {
$parts[] = mb_substr($string, $pos, $chunk_size);
}
print_r($parts);

Выход:

Array
(
[0] => this is my long text this is my long text this
is my long text this is my lon
[1] => g text
this is my long text this is my
long text this is my long tex
[2] => t this is my long text
this is my long text  this is my long text
)
1

Вы можете добиться того, что вы просили, кодируя следующим образом:

$string="this is my long text this is my long text this
is my long text this is my long text
this is my long text this is my
long text this is my long text this is my long text
this is my long text  this is my long text";$strlen = strlen($string);$strlen1 = floor((35 / 100) * $strlen);
$strlen2 = $strlen1 + floor((35 / 100) * $strlen);
$strlen3 = $strlen2 + floor((30 / 100) * $strlen);

echo substr($string,0,$strlen1);
echo"<br>";
echo substr($string,$strlen1,$strlen2);
echo "<br>";
echo substr($string,$strlen2,$strlen3);

Для лучшего использования Вы можете заключить это в функцию, которая возвращает разделенные строки 🙂

0

<?php
$string = "this is my long text this is my long text this
is my long text this is my long text
this is my long text this is my
long text this is my long text this is my long text
this is my long text  this is my long text";
$arr1   = str_split($string);

$pieces = array_chunk($arr1, ceil(count($arr1) / 3));
echo explode($pieces[0],'');// this will convert the array back into string you wish to have.
echo explode($pieces[1],'');
echo explode($pieces[2],'');?>

Я не проверял это. Но так мы можем это сделать.
Сначала разбейте строку на массив и разделите на желаемое количество с помощью array_chunk.

Теперь, если вы печатаете, что $ штук. Вы получите нарезанный вход в каждом индексе.

0
По вопросам рекламы ammmcru@yandex.ru
Adblock
detector