note 30270 added to function.preg-split
| From: | csaba at alum dot mit dot edu | Date: | Wed, 12 Mar 2003 17:53:26 +0000 |
| Subject: | note 30270 added to function.preg-split | ||
| Groups: | php.notes | ||
| Request: | Send a blank email to php-notes+get-45162@lists.php.net to get a copy of this message | ||
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;
}
--
http://www.php.net/manual/en/function.preg-split.php
http://master.php.net/manage/user-notes.php?action=edit+30270
http://master.php.net/manage/user-notes.php?action=delete+30270
http://master.php.net/manage/user-notes.php?action=reject+30270