note 89138 added to function.explode
| From: | adrian at bilsoftware dot com | Date: | Mon, 23 Feb 2009 22:40:58 +0000 |
| Subject: | note 89138 added to function.explode | ||
| Groups: | php.notes | ||
| Request: | Send a blank email to php-notes+get-151038@lists.php.net to get a copy of this message | ||
<?php
function explode_escaped($delimiter, $string){
$exploded = explode($delimiter, $string);
$fixed = array();
for($k = 0, $l = count($exploded); $k < $l; ++$k){
if($exploded[$k][strlen($exploded[$k]) - 1] == '\\') {
if($k + 1 >= $l) {
$fixed[] = trim($exploded[$k]);
break;
}
$exploded[$k][strlen($exploded[$k]) - 1] = $delimiter;
$exploded[$k] .= $exploded[$k + 1];
array_splice($exploded, $k + 1, 1);
--$l;
--$k;
} else $fixed[] = trim($exploded[$k]);
}
return $fixed;
}
?>
Here's a function which explodes string with delimiter, but if delimiter is "escaped"
by backslash, function won't split in that point. Example:
<?php
$result = explode_escaped(',', 'string, piece, group\, item\, item2,
next\,asd');
print_r($result);
?>
Will give:
Array
(
[0] => string
[1] => piece
[2] => group, item, item2
[3] => next,asd
)
----
Server IP: 82.177.164.100
Probable Submitter: 83.22.24.219
----
Manual Page -- http://www.php.net/manual/en/function.explode.php
Edit -- https://master.php.net/note/edit/89138
Del: integrated -- https://master.php.net/note/delete/89138/integrated
Del: useless -- https://master.php.net/note/delete/89138/useless
Del: bad code -- https://master.php.net/note/delete/89138/bad+code
Del: spam -- https://master.php.net/note/delete/89138/spam
Del: non-english -- https://master.php.net/note/delete/89138/non-english
Del: in docs -- https://master.php.net/note/delete/89138/in+docs
Del: other reasons-- https://master.php.net/note/delete/89138
Reject -- https://master.php.net/note/reject/89138
Search -- https://master.php.net/manage/user-notes.php