Blank space at the end of each array index?

0

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.

But when I enter the array, I end up with a blank space for each row in the array, for example:

Array(0)=> 'eu irei '

Array(1)=> 'ser '

When it was meant to be:

Array(0)=> 'eu irei'

Array(1)=> 'ser'

This causes an error when comparing the test. How do I remove this white space at the end of each line?

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]." ";
}
    
asked by anonymous 23.09.2018 / 20:09

1 answer

2

To remove spaces at the beginning and end of a string you can use the trim function. If you want to remove only at the end you have a variant of trim that you can use that is called rtrim (the name comes from right trim). These functions return the new string instead of changing the original.

The usage is simple as:

$stringSemEspacoNoFim = rtrim ($string);

Applying to your code:

if($limit<strlen($string)){
    $arr[$line] = rtrim($arr[$line]); //aplicação do trim no fim de construir a linha

    //line++ for inserting the string on a next index
    $line++;
    
24.09.2018 / 12:32