tags:

views:

105

answers:

5

i have string like this

$string =  'aaaaaa, bbbbbb, cccccc, ';

and i want to modified it to be like this

$string = 'aaaaaa, bbbbbb, cccccc';

the last ',' and space is removed.

how to do this in php? what is the function needed the achieve that?


my full code is like this

if(isset($_POST['accomodation']))   $accomodation = 'Accomodation, ';
if(isset($_POST['dance']))          $dance = 'Dance Lessons, ';
if(isset($_POST['vacation']))       $vacation   = 'Vacation planning, ';
if(isset($_POST['group']))      $group = 'Group Vacation, ';
if(isset($_POST['inprivate']))      $inprivate = 'Private Vacation, ';
if(isset($_POST['land']))           $land = 'Land purchase/lease';
if(isset($_POST['all']))            $all    = 'All';

@$interest = $accomodation.$dance.$vacation.$group.$inprivate.$land;
@echo $string;

*sorry for such dumb question, it's been so long i didn't touch native PHP programming

A: 

Is it always a comma then a space at the end?

substr($string, 0, -2)
Ergo Summary
given the subject line says "remove last string IF its ', '" I assume not
Paul
+6  A: 

rtrim() function:

rtrim($string,', ');

but how are you defining the string? It may be that you can build it without the comma and space.

EDIT

$interests = array();
if(isset($_POST['accomodation']))   $interests[] = 'Accomodation'; 
if(isset($_POST['dance']))          $interests[] = 'Dance Lessons'; 
if(isset($_POST['vacation']))       $interests[] = 'Vacation planning'; 
if(isset($_POST['group']))          $interests[] = 'Group Vacation'; 
if(isset($_POST['inprivate']))      $interests[] = 'Private Vacation'; 
if(isset($_POST['land']))           $interests[] = 'Land purchase/lease'; 
if(isset($_POST['all']))            $all    = 'All'; 

$interest = implode(', ',$interests);
echo $interest;
Mark Baker
Will also trim things like ",,,,," and " , , , " but maybe that's what's wanted...
Paul
i update the code, to make it more understanding :)
GusDe CooL
already try this `rtrim($string,', ');` but i didn't remove the last `,` only remove the last space :(
GusDe CooL
Consider using `implode` if you want to concatenate with a separator.
Alin Purcaru
@GusDe CooL - If $interest = rtrim($interest,', '); didn't remove the comma, then either you're using a very old version of PHP (earlier than 4.1.0) or you're not calling rtrim() correctly, or you have a very broken version of PHP
Mark Baker
the `$interest = rtrim($interest,', ');` did do task, what i do in the first that only `rtrim($interest,', ');` as the first i see your code. thanks you very much for your help :)
GusDe CooL
+1  A: 
$string = preg_replace( "/,\s*$/","",$string);

Should do the trick

Cfreak
this would def work- though it is usually better practice to use PHP string functions in place of RegEx where possible- as they execute faster. However, the difference is fairly miniscule
Ergo Summary
@Ergo you're absolutely right. I just happen to like regexes :)
Cfreak
@Cfreak, know what you mean, I have a bit of a thing for them too....(probably the fact they work across languages etc..)
Ergo Summary
+4  A: 
$string = preg_replace('/\s*,\s*$/', '', $string);

or, way cooler:

$string = rtrim($string, " ,");

Note that it does not matter the order of the characters in the pattern string.

@You last update. This changes some things. You could put all your variables in one array and then implode it. Like so:

$items = array();
$items[] = $accomodation = 'Accomodation';
$items[] = $dance = 'Accomodation';
...
$result = implode(', ', $items) 
Alin Purcaru
Thanks you very much.. the completed code really help much..
GusDe CooL
A: 

Often times, you can avoid the trailing comma altogether by changing the way you build the string. For example:

$count = 0;
foreach ($this as $that) {

   if ($count != 0) {
      $string .= ',';
   }

   $string .= $that['stuff'];
   $count++;
}

Would remove the possibility of any trailing comma at the end, no matter the combination of results.

bpeterson76
Mh, I have seen this pattern and used it myself, but see @[Mark Baker]s answer: assigning the items to an array and join()ing them with the desired separator is a much better and cleaner way to do this.
Max