Help triming a string
Moderator: General Moderators
-
farfromrest
- Forum Newbie
- Posts: 11
- Joined: Sun Jul 20, 2003 7:09 pm
Help triming a string
I need to figure out some way to trim of the last , in a string.
-
__autoload()
- Forum Newbie
- Posts: 7
- Joined: Tue Jul 15, 2003 10:43 am
Code: Select all
<?php
$string = 'Space ';
$string = rtrim($string);
//$string = 'Space';
?>
Last edited by __autoload() on Mon Jul 21, 2003 3:19 am, edited 1 time in total.
To remove the last , use substr();
That should work.. 
Code: Select all
<?php
$text = "one, two, three,";
$text = substr($text, 0, -1);
echo($text);
?>-
__autoload()
- Forum Newbie
- Posts: 7
- Joined: Tue Jul 15, 2003 10:43 am
Oh, you mean remove the last comma..
Gen-ik, that code you gave wouldn't work if the string was :
It'd only remove the last character. I think you'd have to use pattern matching for this...
Gen-ik, that code you gave wouldn't work if the string was :
Code: Select all
<?php
$string = 'one , two ,three';
?>Good point. Ok.. so let's check to see if the last character is a , before we attempt to chop it off....
By the way the trim() command simply chops off any 'white space' from the begining and ends of a variable... not always needed but it keeps your text tidy.
Code: Select all
<?php
$text = "one, two, three,";
if(substr(trim($text), 0, -1)==",")
{
$text = substr(trim($text), 0, -1);
}
echo($text);
?>By the way the trim() command simply chops off any 'white space' from the begining and ends of a variable... not always needed but it keeps your text tidy.