Explode a string and return an array with no empty elements


/ Published in: PHP
Save to your folder(s)



Copy this code and paste it in your HTML
  1. <?php
  2. /*
  3. Function: eexplode
  4.  
  5. Explodes an string and removes any empty
  6. elements in the resulting array
  7.  
  8. Parameters:
  9. $separator - The separator used to explode the $string
  10. $string - The string to explode
  11.  
  12. Returns:
  13. An array with no empty elements
  14.  
  15. Example:
  16. print_r(explode(":","evan:walsh:"));
  17.  
  18. Array(
  19. [0] => evan
  20. [1] => walsh
  21. [2] =>
  22. );
  23.  
  24. print_r(eexplode(":","evan:walsh:"));
  25.  
  26. Array(
  27. [0] => evan
  28. [1] => walsh
  29. );
  30.  */
  31. function eexplode($separator,$string){
  32. $array = explode($separator,$string);
  33. foreach($array as $key => $val){
  34. if(empty($val)){
  35. unset($array[$key]);
  36. }
  37. }
  38. return $array;
  39. }

URL: http://nothingconcept.com

Report this snippet


Comments

RSS Icon Subscribe to comments

You need to login to post a comment.