multi exploding a string


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

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


Copy this code and paste it in your HTML
  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


Comments

RSS Icon Subscribe to comments

You need to login to post a comment.