Page 1 of 1

chopping the end of a string off

Posted: Thu Jan 29, 2004 4:04 pm
by mccommunity
I have a string and it varys in size but the last part of it will always be a comma and I need to strip that off. So basically I am looking for a function that will strip the last character off a string. Thanks.

Posted: Thu Jan 29, 2004 4:50 pm
by Pointybeard
Well the first place you should look is the PHP documentation. :p heres the link - http://www.php.net/manual/en/

There are heaps of ways to do what you want. Functions that would interest you would be preg_replace(), strlen(), substr()...actually have a look at the list of string functions. heres the link... http://au.php.net/manual/en/ref.strings.php

Essentially all you are going to need do is either replace comma's with "", or use substr and strlen to work out how to chop off the last char.

-PB

Posted: Thu Jan 29, 2004 4:57 pm
by DuFF
The easiest would probably be to use substr. Heres an example from php.net/substr:

Code: Select all

<?php
$rest = substr("abcdef", 0, -1);  // returns "abcde"
?>