Remove the last <br /> of a given string - issues

This is something to do with the ckeditor, it keeps adding a <br /> at the end and, for any reason, I cannot get rid of it consistently.

Sometimes I get a < at the end of my string;
Sometimes I get all well cleared.

I’m using this at the moment, but perhaps the lack of consistency could have something to do with the white spaces that could have been added or somethinhg?

Anyway, here’s the thing:

substr_replace($noticia->primeiro_paragrafo_noticia ,"",-7);

Is there a way do this on a way that we can garantee that there are no whitespaces involved? Could we trim, and then do this substr_replace and then “un trim” or something?

I have tried like so as well, but here I cannot get any removal at all:

rtrim($noticia->primeiro_paragrafo_noticia, '<br />');

Thanks a lot,
Marcio

You could try a:


rtrim(trim($var), '<br />');

Worked like a charm! :wink: How have you came with this idea?

Thanks a lot,
Márcio

rtrim is not the correct function to use here imho.
Consider the following:


brrrrrrrrrr<br />

Since rtrim removes all characters that are in the second argument occuring at the end of the string the whole text above would be removed (since all the characters therein are in ‘<br />’)

A better way would be to use


$var = preg_replace('/<br\\s*?\\/?>\\s*$/', '', $var);