note 102590 deleted from function.explode by googleguy

From: Date: Thu, 22 Nov 2012 16:55:00 +0000
Subject: note 102590 deleted from function.explode by googleguy
References: 1  Groups: php.notes 
Request: Send a blank email to php-notes+get-192094@lists.php.net to get a copy of this message
Note Submitter: jehoshua02 at gmail dot com ---- If you are looking for a simpler multi-explode that returns a flat array, use a regular expression to standardize all delimiters before exploding. <?php // function to explode on multiple delimiters function multi_explode($pattern, $string, $standardDelimiter = ':') { // replace delimiters with standard delimiter, also removing redundant delimiters $string = preg_replace(array($pattern, "/{$standardDelimiter}+/s"), $standardDelimiter, $string); // return the results of explode return explode($standardDelimiter, $string); } // test // set up variables $string = "zero one | two :three/ four\ five:: six|| seven eight nine\n ten \televen twelve thirteen fourteen fifteen"; $pattern = '/[:\|\\\\\/\s]/'; // colon (:), pipe (escaped '\|'), slashes (escaped '\\\\' and '\/'), white space (\s) // call function $result = multi_explode($pattern, $string); // display results var_dump($result); ?> Output: array 0 => string 'zero' (length=4) 1 => string 'one' (length=3) 2 => string 'two' (length=3) 3 => string 'three' (length=5) 4 => string 'four' (length=4) 5 => string 'five' (length=4) 6 => string 'six' (length=3) 7 => string 'seven' (length=5) 8 => string 'eight' (length=5) 9 => string 'nine' (length=4) 10 => string 'ten' (length=3) 11 => string 'eleven' (length=6) 12 => string 'twelve' (length=6) 13 => string 'thirteen' (length=8) 14 => string 'fourteen' (length=8) 15 => string 'fifteen' (length=7) See: http://www.php.net/manual/en/book.pcre.php

« previous php.notes (#192094) next »