php 中带下划线的 rtrim
我的行是“Delux Room_room”。我想要得到“豪华房”。我尝试了以下代码。
以下代码给出以下结果。
echo $row.'<br/>';
echo rtrim($row, 'room') .'<br/>';
echo rtrim($row, '_room') .'<br/>';
结果;-
Delux Room_room
Delux Room_
Delux R
但我想要豪华房。请帮我。
I have line as 'Delux Room_room'. I want get 'Delux Room' as result. I tried following codes.
Following code give following result.
echo $row.'<br/>';
echo rtrim($row, 'room') .'<br/>';
echo rtrim($row, '_room') .'<br/>';
Result ;-
Delux Room_room
Delux Room_
Delux R
But i want Delux Room. Please help me.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(4)
这表示“从字符串末尾删除字符,直到找到不是
_
、r
、o
或的字符。米。”这显然不是你的意思。来自 PHP 手册:
您提供一个字符列表,而不是要删除的字符串。
您可能应该使用
substr
代替:另一种解决方案是使用
str_replace
替换不需要的子字符串:请注意,这不如
substr
方法。That says "remove characters from the end of the string until you get to one that isn't
_
,r
,o
orm
." Which obviously isn't what you mean. From the PHP manual:You provide a list of characters, not a string you want to remove.
You should probably use
substr
instead:Another solution would be to use
str_replace
to replace unwanted substrings:Note that this is less precise than the
substr
approach.rtrim
的第二个参数是一组字符。您必须编写自己的方法来删除特定字符串:rtrim
's second argument is a set of characters. You'll have to write your own method to remove a specific string instead:作为@lonesomeday 的解决方案的替代方案,如果您的过去的长度是可变的:
Alternatively to @lonesomeday's solution, if your last past is of variable length:
您应该使用
str_replace
而不是rtrim
,如下所示:You should use
str_replace
instead ofrtrim
, like this: