Remove 3 chars from the end of a string

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
jwalsh
Forum Contributor
Posts: 202
Joined: Sat Jan 03, 2004 4:55 pm
Location: Cleveland, OH

Remove 3 chars from the end of a string

Post by jwalsh »

Hi,

I have a string that is generated through a while loop. The result is like this..

aaa - bbb - ccc - ddd - eee - fff -

obviously, aaa, bbb,etc, are just placeholders, but my question is , how can I remove the last " - " but leave the ones inside?

Thanks,

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

Post by feyd »

[php_man]substr()[/php_man] ... you can also instead of blindly tacking on the text to add all the values into an array and use [php_man]join()[/php_man]
Drachlen
Forum Contributor
Posts: 153
Joined: Fri Apr 25, 2003 1:16 am

Post by Drachlen »

Code: Select all

<?php

	$string = "aaa - bbb - ccc - ddd - eee - fff - ";

	echo substr($string, 0, -3);

?>
[php_man]substr[/php_man] to see other uses :D
User avatar
pickle
Briney Mod
Posts: 6445
Joined: Mon Jan 19, 2004 6:11 pm
Location: 53.01N x 112.48W
Contact:

Post by pickle »

[php_man]rtrim[/php_man]() works too:

Code: Select all

$trimmed = rtrim($string,' - ');
Real programmers don't comment their code. If it was hard to write, it should be hard to understand.
Post Reply