Posted By

braunsHizzzle on 04/08/09


Tagged

delete String last comma


Versions (?)

Who likes this?

4 people have marked this snippet as a favorite

braunsHizzzle
jlvallelonga
vali29
BrianCoyDesign


Delete Last Comma in a string


 / Published in: PHP
 

URL: http://www.totaldream.org

This is the PHP equivalent to http://snipplr.com/view/12074/delete-last-comma/

You will use only the deleteLastComma() function!

  1. function instrrev($n,$s) {
  2. $x=strpos(chr(0).strrev($n),$s)+0;
  3. return (($x==0) ? 0 : strlen($n)-$x+1);
  4. }
  5.  
  6. function mid($s,$x,$w="") { //$w is optional
  7. if ($w) {return substr($s,$x-1,$w);}
  8. else {return substr($s,$x-1);}
  9. }
  10.  
  11. function deleteLastComma($valStr) {
  12. $lastCommaPos = instrrev($valStr, ",");
  13. $valStr = mid($valStr, 1, $lastCommaPos - 1);
  14. return $valStr;
  15. }

Report this snippet  

Comments

RSS Icon Subscribe to comments
Posted By: MMDeveloper on April 8, 2009

you could probably do this with a one-liner regular expression like this (shooting from the hip, untested)

function deleteLastComma($str) { return preg_replace("/(.*),([^,]+)$/", "$1$2", $str); }

You need to login to post a comment.