I'm performing a line break without using the wordwrap()
function.
I'll get a string with the text, and an int with the length of characters allowed by rows as a parameter. I'll have to cut that string, then insert it into an already cut vector. The conditions for cutting are:
I think that probably the error is in the conditions but I can not identify it, some help?
When printing the vector, I get:
Array (0) = > 'If I saw more'
Array (1) = > 'far gone'
Array (2) = > 'to be'
Array (3) = > 'standing'
Array (4) = > 'on'
Array (5) = > 'shoulders of'
Array (6) = > 'giants'
However, I need to print:
Array (0) = > 'If I saw more'
Array (1) = > 'far gone'
Array (2) = > 'to be from'
Array (3) = > 'foot on'
Array (4) = > 'shoulders of'
Array (5) = > 'giants'
Here is the code I developed below:
class Resolution implements TextWrapInterface {
public function textWrap(string $text,int $length):array {
//local variables
$words=explode(" ",$text); //separate the text into words
$arr=array(); //array used for return
$string=" ";
$limit=$length; //limit of characters per line
$line=0;//array line
for($i = 0; $i < count($words); $i++){
$string = $words[$i]." ";
if((strlen($words[$i])>$length)){
//cut the world and print the remaining letters on the next line
$this->cutWord($arr,$words[$i],$limit,$length,$line);
}else
if($limit>=strlen($string)){
//add the word in array line
$arr[$line]=(array_key_exists($line,$arr))?
$arr[$line].$string:$string;
//subtract the limit with the quantity of characters
$limit-=strlen($string);
}else
if($limit<strlen($string)){
//line++ for inserting the string on a next index
$line++;
$limit=$length;
//add the word on array line
$arr[$line]=$string;
//subtract the limit with the quantity of characters
$limit-=strlen($string);
}
}
return $arr;
print_r($arr);
}
//and then I've got a cutWord function
private function cutWord(&$array,$word,&$limit,$length,$index){
for($i = 0; $i < strlen($word); $i++){
//verify if the index doesn't have any words in
if(($limit!=$length)&&($i==0)){
$index++; // jump an array line
$limit=$length; //limit receives starting value
}
//verify if the limit is > 0
if($limit<=0) {
$index++;
$limit=$length; //limit receives starting value
}
//add the letter in the array index concatenating with the previous
$array[$index]=(array_key_exists($index,$array))?
$array[$index].$word[$i]:$word[$i];
$limit--;
}
$array[$index]=$array[$index]." ";
}