0

Can anyone tell me how I would remove a substring from the end of a string.
The string in question could have any one of three substrings at the end of it that need removed, so I wouldn't know in advance what the length was.

2
Contributors
2
Replies
4
Views
6 Years
Discussion Span
Last Post by HelenLF
0
$treplace = array("string1","string2","string3");
$twith=array("","","");
$done=str_replace($treplace,$twith,$origstring);

Like that? Or do you want it only replaced if it's at the end of the string? If you're looking for extensions, you might use the strrpos function and look for the '.' then use substr to remove everything after it.

0

Great!
Thanks for the quick reply. That does exactly what I want.
The substrings in question only ever appear at the end of the string so this is fine as it is.
Thanks again.

This question has already been answered. Start a new discussion instead.
Have something to contribute to this discussion? Please be thoughtful, detailed and courteous, and be sure to adhere to our posting rules.