09 Oct remove last character from string in php Posted by Matt | 29 Comments
I tried trim but did not work. Thnx

29 Replies to "remove last character from string in php"

jiji July 29, 2010 at 1:28 pm | Reply

+2

You have to use like this

$var = substr($var,0,-1);
BillyBobsCat August 19, 2010 at 6:09 pm | Reply

0

syntax:

mixed substr_replace ( mixed $string , string $replacement , int $start [, int $length ] )

e.g.
/* These next two replace 'MNRPQR' in $var with 'bob'. */
echo substr_replace($var, 'bob', 10, -1) . "<br />\n";
echo substr_replace($var, 'bob', -7, -1) . "<br />\n";

From the manual

Note, using this I had to use -3 to remove a character and \r\n off a string
jhice November 18, 2010 at 4:15 pm | Reply

0

+1 !

Posted by N on Oct 29th, 2008

Actually, trim can work. Specifically, rtrim.

echo rtrim('remove the letter a', 'a');
Roni November 23, 2010 at 2:34 pm | Reply

0

Its not readable... Keep the font lil bigger....
Nate Purser January 21, 2012 at 2:20 am | Reply

0

From the PHP Manual documentation for the substr function.

$rest = substr(\"abcdef\", 0, -1); // returns \"abcde\"

Someone else posted it, but I wanted to second that I think it\'s the most straightforward way.
Maharaj March 25, 2012 at 12:26 pm | Reply

0

Dear Nex...

Thank you for your updates. It working fine.........
mohit August 31, 2012 at 1:34 pm | Reply

0

ya its nice work


Please be polite and helpful and do not spam or offend others. We promise you will be treated the same way.

Log in your free account or if you still haven't joined our Webmaster Community Reward Program, you can create your free account now.

Posting tip:
if you use code in your comments please put it in these tags [php], [sql], [css], [js]
PHP code example: [php] echo date("Y-m-d"); [/php]

Thank you,
~ PHPJabbers team ~