chopping the end of a string off

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
mccommunity
Forum Commoner
Posts: 62
Joined: Mon Oct 07, 2002 8:55 am

chopping the end of a string off

Post 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.
User avatar
Pointybeard
Forum Commoner
Posts: 71
Joined: Wed Sep 03, 2003 7:23 pm
Location: Brisbane, AUS
Contact:

Post 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
User avatar
DuFF
Forum Contributor
Posts: 495
Joined: Tue Jun 24, 2003 7:49 pm
Location: USA

Post 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"
?>
Post Reply