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

iam_peter picture iam_peter · Jan 6, 2011 · Viewed 18.9k times · Source

as the title says i have a string like this:

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

now i want to get rid of the <br>s at the end of this string so it looks like this:

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

I tried this:

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

but this did not work. maybe someone knows the right regex.

greetings peter

Answer

Alex Howansky picture Alex Howansky · Jan 6, 2011

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);