delete latest character in string if its a dot?

PHP programming forum. Ask questions or help people concerning PHP code. Don't understand a function? Need help implementing a class? Don't understand a class? Here is where to ask. Remember to do your homework!

Moderator: General Moderators

Post Reply
User avatar
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?

Post 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
User avatar
superdezign
DevNet Master
Posts: 4135
Joined: Sat Jan 20, 2007 11:06 pm

Post by superdezign »

Code: Select all

if ($str[strlen($str) - 1] == '.') {
    $str = substr($str, 0, strlen($str) - 1);
}
User avatar
John Cartwright
Site Admin
Posts: 11470
Joined: Tue Dec 23, 2003 2:10 am
Location: Toronto
Contact:

Post 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
User avatar
VladSun
DevNet Master
Posts: 4313
Joined: Wed Jun 27, 2007 9:44 am
Location: Sofia, Bulgaria

Post by VladSun »

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
User avatar
RobertGonzalez
Site Administrator
Posts: 14293
Joined: Tue Sep 09, 2003 6:04 pm
Location: Fremont, CA, USA

Post 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';
}

?>
User avatar
feyd
Neighborhood Spidermoddy
Posts: 31559
Joined: Mon Mar 29, 2004 3:24 pm
Location: Bothell, Washington, USA

Post by feyd »

I'd use rtrim() here. :)
User avatar
Kieran Huggins
DevNet Master
Posts: 3635
Joined: Wed Dec 06, 2006 4:14 pm
Location: Toronto, Canada
Contact:

Post by Kieran Huggins »

rtrim()/feyd FTW
User avatar
RobertGonzalez
Site Administrator
Posts: 14293
Joined: Tue Sep 09, 2003 6:04 pm
Location: Fremont, CA, USA

Post 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.
User avatar
John Cartwright
Site Admin
Posts: 11470
Joined: Tue Dec 23, 2003 2:10 am
Location: Toronto
Contact:

Post by John Cartwright »

feyd wrote:I'd use rtrim() here. :)
doh!
User avatar
Kieran Huggins
DevNet Master
Posts: 3635
Joined: Wed Dec 06, 2006 4:14 pm
Location: Toronto, Canada
Contact:

Post by Kieran Huggins »

Everah++

(feyd++ by proxy)
Post Reply