delete latest character in string if its a dot?
Moderator: General Moderators
- potato
- Forum Contributor
- Posts: 192
- Joined: Tue Mar 16, 2004 8:30 am
- Location: my lovely trailer, next to the big tree
delete latest character in string if its a dot?
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
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
- superdezign
- DevNet Master
- Posts: 4135
- Joined: Sat Jan 20, 2007 11:06 pm
Code: Select all
if ($str[strlen($str) - 1] == '.') {
$str = substr($str, 0, strlen($str) - 1);
}- John Cartwright
- Site Admin
- Posts: 11470
- Joined: Tue Dec 23, 2003 2:10 am
- Location: Toronto
- Contact:
Code: Select all
$strlen = strlen($string)-1;
if (strrpos($string, '.') == $strlen) {
$string = substr($string, 0, $strlen);
}Regexp solution:
Code: Select all
$string = preg_replace("/^(.+)[.]$/s", '$1', $string);There are 10 types of people in this world, those who understand binary and those who don't
- RobertGonzalez
- Site Administrator
- Posts: 14293
- Joined: Tue Sep 09, 2003 6:04 pm
- Location: Fremont, CA, USA
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';
}
?>- Kieran Huggins
- DevNet Master
- Posts: 3635
- Joined: Wed Dec 06, 2006 4:14 pm
- Location: Toronto, Canada
- Contact:
- RobertGonzalez
- Site Administrator
- Posts: 14293
- Joined: Tue Sep 09, 2003 6:04 pm
- Location: Fremont, CA, USA
feyd++. For some reason I was reading that as compare, not delete. rtrim() is the best choice for removing the last character hands down.
- John Cartwright
- Site Admin
- Posts: 11470
- Joined: Tue Dec 23, 2003 2:10 am
- Location: Toronto
- Contact:
doh!feyd wrote:I'd use rtrim() here.
- Kieran Huggins
- DevNet Master
- Posts: 3635
- Joined: Wed Dec 06, 2006 4:14 pm
- Location: Toronto, Canada
- Contact: