Page 1 of 1

Remove 3 chars from the end of a string

Posted: Tue Jan 04, 2005 7:28 pm
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

Posted: Tue Jan 04, 2005 7:37 pm
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]

Posted: Tue Jan 04, 2005 7:40 pm
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

Posted: Wed Jan 05, 2005 9:47 am
by pickle
[php_man]rtrim[/php_man]() works too:

Code: Select all

$trimmed = rtrim($string,' - ');