Reputation: 949
I have a string containing some text, the last character might (might) be a slash, which I don't want. How do I remove that, if it exists?
Is this the "correct" way?
if(substr($str, -1) == "/") $str = rtrim($str, '/');
Upvotes: 3
Views: 2953
Reputation: 1549
Use rtrim
with two parameters (string to be trimmed, character to be removed)
For your case:
rtrim($str, '/');
Upvotes: 0
Reputation: 1687
In my case i need to delete severalsymbols like "1." or "2.", etc. Here's code:
/**
* @param string $string
*
* @return string
*/
private function cutNumbers($string)
{
for ($i = 1; $i < 20; $i++) {
$position = strpos($string, $i . '.');
if ($position === 0) {
$string = substr($string, 2);
}
}
return $string;
}
PS: I know there is better solution but anyway - hotfix will help someone.
Upvotes: 0
Reputation: 1
This replaces the last character of a url if it is a '/'
concat(LEFT(url, LENGTH(url)-1),replace(right(url, 1),'/',''))
Upvotes: 0
Reputation: 2140
if you have a string $string try using
substr_replace($string ,"",-1);
or
substr($string, 0, -1);
or
mb_substr($string, 0, -1);
it will remove the last character from $string.
Upvotes: 1
Reputation: 437326
Use rtrim
without a condition, it's shorter and probably faster as well. The added if
is noise and doesn't offer anything.
Upvotes: 11