note 30270 deleted from function.preg-split by sniper

From: Date: Tue, 30 Sep 2003 12:34:43 +0000
Subject: note 30270 deleted from function.preg-split by sniper
References: 1  Groups: php.notes 
Request: Send a blank email to php-notes+get-57491@lists.php.net to get a copy of this message
Note Submitter: csaba@alum.mit.edu ---- If you have a string with a certain character 'protected' by a preceding backslash, the function below allows you to split on only the unprotected character (if you have a solution with preg_split or preg_match, I'd love to hear about it). Example: "asdf\\-qwer-\\\\-poiu-" should be split into "asdf\\-qwer", "\\\\", "poiu", and "" Csaba Gabor from New York function explodeSafe($delim, $str) { // explodes $str around $delim, except those $delim // preceded by an odd number of backslashes for ($i=sizeof($aExploded=explode($delim, $str))-2;$i>=0;--$i) if (preg_match("/\\\\*$/D", $aExploded[$i], $mtch) && (strlen($mtch[0]) % 2)) { $aExploded[$i] .= $delim.$aExploded[$i+1]; array_splice ($aExploded, $i+1, 1); } return $aExploded; }

« previous php.notes (#57491) next »