Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Remove <br>'s from the end of a string

As the title says I have a string like this:

$string = "Hello World<br>hello world<br><br>";

I would like to get rid of the <br>s at the end of the string so it looks as follows:

$string = "Hello World<br>hello world";

I tried this:

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

but it didn't work. Maybe someone knows the right regex.

like image 837
iam_peter Avatar asked Jan 06 '11 18:01

iam_peter


1 Answers

You're close, you used ^ at the start of the regexp, which means "match the start of the string." You want $ at the end, which means, "Match the end of the string."

preg_replace('/(<br>)+$/', '', $string);
like image 148
Alex Howansky Avatar answered Sep 20 '22 19:09

Alex Howansky