Welcome to ShenZhenJia Knowledge Sharing Community for programmer and developer-Open, Learning and Share
menu search
person
Welcome To Ask or Share your Answers For Others

Categories

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.

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
thumb_up_alt 0 like thumb_down_alt 0 dislike
231 views
Welcome To Ask or Share your Answers For Others

1 Answer

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

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
thumb_up_alt 0 like thumb_down_alt 0 dislike
Welcome to ShenZhenJia Knowledge Sharing Community for programmer and developer-Open, Learning and Share
...