Split by characters of a paragraph into 3 equal parts in PHP

737 views Asked by At

If i have a paragraph i need it to divide it into 3 equal parts (by characters).

This is my string

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

What i need is:

  • Equal characters in 3 parts or
  • 35% in part1, 35% in part2 and 30% in part3 (3 parts - by characters not by word or string)

Any experts ?

4

There are 4 answers

3
Sanooj T On BEST ANSWER

Check this one also

$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));
0
balu anand On

You can achieve what you asked by coding as follows:

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

For better usage You can wrap it up in function which returns splitted strings :)

0
Ashok On
<?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],'');


?>

I haven't tested it. But this way we can do it. First split the string into array and divide into number of half you wish with array_chunk.

Now if you print that $pieces. You will get the sliced input in each index.

0
RomanPerekhrest On

The solution using mb_strlen() and mb_substr() functions:

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

The output:

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
)