Skip to content
Advertisement

rtrim with underscore in php

I have line as ‘Delux Room_room’. I want get ‘Delux Room’ as result. I tried following codes.

Following code give following result.

JavaScript

Result ;-

JavaScript

But i want Delux Room. Please help me.

Advertisement

Answer

JavaScript

That says “remove characters from the end of the string until you get to one that isn’t _, r, o or m.” Which obviously isn’t what you mean. From the PHP manual:

$charlist You can also specify the characters you want to strip, by means of the charlist parameter. Simply list all characters that you want to be stripped. With .. you can specify a range of characters.

You provide a list of characters, not a string you want to remove.

You should probably use substr instead:

JavaScript

Another solution would be to use str_replace to replace unwanted substrings:

JavaScript

Note that this is less precise than the substr approach.

User contributions licensed under: CC BY-SA
3 People found this is helpful
Advertisement