Posted By

fackz on 08/13/10


Tagged

String explode multi


Versions (?)

Who likes this?

2 people have marked this snippet as a favorite

fackz
jamalmohamed


multi exploding a string


 / Published in: PHP
 

Here's a function for "multi" exploding a string.

  1. <?php
  2. //the function
  3. //Param 1 has to be an Array
  4. //Param 2 has to be a String
  5. function multiexplode ($delimiters,$string) {
  6. $ary = explode($delimiters[0],$string);
  7. array_shift($delimiters);
  8. if($delimiters != NULL) {
  9. foreach($ary as $key => $val) {
  10. $ary[$key] = multiexplode($delimiters, $val);
  11. }
  12. }
  13. return $ary;
  14. }
  15.  
  16. // Example of use
  17. $string = "1-2-3|4-5|6:7-8-9-0|1,2:3-4|5";
  18. $delimiters = Array(",",":","|","-");
  19.  
  20. $res = multiexplode($delimiters,$string);
  21. echo '<pre>';
  22. print_r($res);
  23. echo '</pre>';

Report this snippet  

You need to login to post a comment.