Page 1 of 1
delete latest character in string if its a dot?
Posted: Sat Oct 20, 2007 2:57 pm
by potato
Hi,
i have to delete the latest dot in a string, but only if the latest character really is a dot.
Otherwise nothing has to heppen with the string.
Does somebody knows how i can do this?
many thanx in advance,
tom
Posted: Sat Oct 20, 2007 3:20 pm
by superdezign
Code: Select all
if ($str[strlen($str) - 1] == '.') {
$str = substr($str, 0, strlen($str) - 1);
}
Posted: Sat Oct 20, 2007 3:22 pm
by John Cartwright
Code: Select all
$strlen = strlen($string)-1;
if (strrpos($string, '.') == $strlen) {
$string = substr($string, 0, $strlen);
}
Loosely put, if the last occurance of the '.' (strrpos) is found to be the length of the string (strlen), then remove the last character
Posted: Sat Oct 20, 2007 3:28 pm
by VladSun
Regexp solution:
Code: Select all
$string = preg_replace("/^(.+)[.]$/s", '$1', $string);
Posted: Sat Oct 20, 2007 4:29 pm
by RobertGonzalez
Code: Select all
<?php
$str0 = 'This is a string that ends in a dot.';
$str1 = 'This is a string that does not. ';
// Try using both strings
$end0 = strlen($str0)-1;
$end1 = strlen($str1)-1;
if ($str0[$end0] == '.') {
echo 'I am dot ender';
} else {
echo 'I am no dotter';
}
echo '<br />';
if ($str1[$end1] == '.') {
echo 'I am dot ender';
} else {
echo 'I am no dotter';
}
?>
Posted: Mon Oct 22, 2007 4:52 pm
by feyd
I'd use
rtrim() here.

Posted: Mon Oct 22, 2007 11:49 pm
by Kieran Huggins
rtrim()/feyd FTW
Posted: Tue Oct 23, 2007 10:17 am
by RobertGonzalez
feyd++. For some reason I was reading that as compare, not delete.
rtrim() is the best choice for removing the last character hands down.
Posted: Tue Oct 23, 2007 10:55 am
by John Cartwright
Posted: Tue Oct 23, 2007 4:08 pm
by Kieran Huggins
Everah++
(feyd++ by proxy)