note 102590 added to function.explode

From: Date: Wed, 23 Feb 2011 07:37:04 +0000
Subject: note 102590 added to function.explode
Groups: php.notes 
Request: Send a blank email to php-notes+get-177243@lists.php.net to get a copy of this message
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 ---- Server IP: 69.147.83.197 Probable Submitter: 96.225.195.147 ---- Manual Page -- http://www.php.net/manual/en/function.explode.php Edit -- https://master.php.net/note/edit/102590 Del: integrated -- https://master.php.net/note/delete/102590/integrated Del: useless -- https://master.php.net/note/delete/102590/useless Del: bad code -- https://master.php.net/note/delete/102590/bad+code Del: spam -- https://master.php.net/note/delete/102590/spam Del: non-english -- https://master.php.net/note/delete/102590/non-english Del: in docs -- https://master.php.net/note/delete/102590/in+docs Del: other reasons-- https://master.php.net/note/delete/102590 Reject -- https://master.php.net/note/reject/102590 Search -- https://master.php.net/manage/user-notes.php

« previous php.notes (#177243) next »