从字符串末尾删除< br [英] remove <br>'s from the end of a string

查看:75
本文介绍了从字符串末尾删除< br的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

正如标题所述,我有一个像这样的字符串:

as the title says i have a string like this:

$string = "Hello World<br>hello world<br><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";

我尝试过:

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

但是这没有用. 也许有人知道正确的正则表达式.

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

问候 彼得

推荐答案

您很接近,您在正则表达式的开头使用了^,表示匹配字符串的开头".您需要在结尾加上$,表示匹配字符串的结尾".

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

这篇关于从字符串末尾删除&lt; br的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆