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

Asked

Viewed 844 times

15

If a string ends with an HTML tag, for example <br> or <hr>, when making use of the PHP function rtrim() to clear said tag, in cases where the letter immediately before is an "r", the same is removed in conjunction 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;

Upshot:

Bubu went to lunch
Bubu went to lunch
Bubu went to lunch (correct)
Bubu went to lunch

Question

How to circumvent this issue so that in the practical case where the condition above occurs, the result does not differ from the expected?

Example in Ideone

4 answers

16


This occurs with trim, ltrim also.

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

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

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

Still, if you want a character range, you can use the ...

Example: a..z to remove lowercase letters.

Handbook: https://www.php.net/manual/en/function.trim.php

Alternative to substrings:

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;

Exit:

<hr>teste

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

There is even an example in comments from the PHP website which does something of the kind, but which can be well simplified in my view.

  • +1 for explaining why it happens

9

You are including 4 items for deletion at the end of your string: <,h,r and >.

Why not make use of the good old regex?

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

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

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

Hugs!

5

You can in a loop/loop check the position of the characters to be removed from the string by using strpos() and "cut off" the string with the function 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 in Ideone

-4

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

  • 3

    Hello, Alisson. Your answer does not answer the question because the idea is to remove the <br> only if it’s at the end, which is why you got negative votes. You can correct your question to try to win positive votes or you can delete it to recover the missed score.

Browser other questions tagged

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