rtrim () to remove "br" also removes the letter "r" if it is the last letter in the string

13

If a string ends with an HTML tag, for example <br> or <hr> , when making use of the PHP function # to clean the tag, in cases where the letter immediately before is an "r", it is removed together with the HTML tag:

Example

$string = "bubu foi almoçar<br>";
echo rtrim($string, "<br>") . PHP_EOL;

$string = "bubu foi almoçar<hr>";    
echo rtrim($string, "<hr>") . PHP_EOL;

$string = "bubu foi almoçar!";
echo rtrim($string, "!") . PHP_EOL;

$string = "bubu foi almoçar";
echo rtrim($string, "<br>") . PHP_EOL;

Result:

  

Bubu was having lunch
  Bubu was having lunch
  Bubu went to lunch (correct)
  Bubu was having lunch

Question

How to work around this issue so that in the practical case where the condition above occurs, the result does not come different than expected?

Example on Ideone

    
asked by anonymous 03.07.2014 / 06:32

4 answers

11

The output is correct in all 4 cases. The problem is the improper use of the second parameter of rtrim :

When you say rtrim( $string, "<hr>" ) , you are saying "remove all occurrences of the characters < , h , r and > from the end of the string, and leave the rest."

Note that r is in the list, so it will be removed as requested.

To remove any other string from the end, replace it like this:

$string = '<hr>teste<hr><hr>';

while( substr( $string, -4) == '<hr>' ) {
   $string = substr( $string, 0, -4 );
}

echo $string;

Output:

<hr>teste
  

This was just a simple example as a starting point. Using arrays can do a function that removes several different groups of strings at a time.

There's even an example in the PHP site comments that does something of the sort , but that can be very simplified in my view.

    
03.07.2014 / 07:29
9

You are including 4 items for deletion at the end of your string: .

Why not make use of the good old regex?

<?= preg_replace('/<hr( \/)?>$/i', '', $string); ?>

This will replace <hr> or <hr /> of the end of the string with nothing.

  

Obs. : Avoid double quotes, as php will process the content. Instead, the use of single quotation marks is preferable, unless   that you want the content to be processed. Ex : <?= "Meu nome é $nome"; ?>

Hugs!

    
03.07.2014 / 18:21
5

You can in a loop check the position of the characters to be removed from the string by using strpos() " and "cut" the string with the substr() ".

You can implement this as follows:

function rtrim2($str, $charlist=null) 
{ 
    $str      = (string)$str; 
    $charlist = (string)$charlist;    

    if(empty($charlist)) 
       return rtrim($str); 

    $len = strlen($charlist); 
    $offset = strlen($str) - $len; 
    while($offset > 0 && $offset == strpos($str, $charlist, $offset)) 
    { 
        $str = substr($str, 0, $offset); 
        $offset = strlen($str) - $len; 
    } 

    return rtrim($str);    
}

Example at IdeOne

    
03.07.2014 / 15:27
-1

Try to use str_replace("<br>", "". $palavra);

    
03.07.2014 / 07:01