Why isn’t my condition giving me the expected result?

Asked

Viewed 39 times

0

I’m performing a line break without using the function wordwrap().

I will receive a string with the text, and an int with the allowed character length per line as parameter. I will have to cut this string, then insert it into an already cut vector. The cutting conditions are:

  1. If a word does not fit on the line and its length is less than the character limit, it should not be cut, but played to the next line.
  2. If the word is greater than the character limit per line, cut the word and continue to print it on the next line.

I think probably the error is in the conditions but I’m not able to identify it, some help?

When printing the vector, I get:

Array(0)=>'If I saw more'
Array(1)=>'far gone'
Array(2)=>'to be'
Standing array(3)=>''
Array(4)=>'about'
Array(5)=>'shoulders of'
Array(6)=>'giants'

However, I need to print:

Array(0)=>'If I saw more'
Array(1)=>'far gone'
Array(2)=>'by being'
Array(3)=>'standing on top'
Array(4)=>'shoulders of'
Array(5)=>'giants'

Follow 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]." ";
}
  • What is the value you are passing to length?

  • I’m passing 12.

  • Pass 13 then. 12 cuts the string that should be the expected result in Array(2) (- To be of -)

No answers

Browser other questions tagged

You are not signed in. Login or sign up in order to post.