Giuseppe Lodi Rizzini
Giuseppe Lodi Rizzini

Reputation: 1055

Rtrim in php remove last <br>

I've a very strange problem. my script cut the last > and i don't know why.

<?php

$string = "<a href='#'>Test</a>" . "<br>";
$string = rtrim($string, "<br>");

var_dump($string);

// OUTPUT

string '<a href='#'>Test</a' (length=19)

// INSTEAD

string '<a href='#'>Test</a>' (length=20)

I need to remove THE LAST <br> in the string (if present) and only if is the tail of the string.

Example:

$string = "<a>CC</a><br><a>CC</a>" //is ok
$string = "<a>CC</a><br><a>CC</a><br>" // --> <a>CC</a><br><a>CC</a>

Upvotes: 1

Views: 499

Answers (1)

Praveen Kumar Purushothaman
Praveen Kumar Purushothaman

Reputation: 167172

The second parameter is not a delimiter but a character mask. So, it will definitely trim off any of the strings, individually.

You can also specify the characters you want to strip, by means of the character_mask parameter. Simply list all characters that you want to be stripped. With .. you can specify a range of characters.

You have to remove it with either str_replace(), if there's only one <br>:

str_replace("<br>", "", $string);

Or you need to use RegExp:

preg_replace('/<br>$/', "", $string);

Upvotes: 3

Related Questions