note 57817 deleted from function.explode by bjori
| From: | bjori@php.net | Date: | Mon, 25 Sep 2006 08:07:59 +0000 |
| Subject: | note 57817 deleted from function.explode by bjori | ||
| References: | 1 | Groups: | php.notes |
| Request: | Send a blank email to php-notes+get-117576@lists.php.net to get a copy of this message | ||
Note Submitter: michael dot martinek at gmail dot com
----
Re: My Last Post
I left an important part of my last post out. My previous explode_quote method will leave the
encapsulation characters in the resulting arrays.
The time average on 4 executions for this modification is 68.66882109642. (On explode() it's 3
seconds, which has almost made me want to write a module for things like this, so they can be
binary-compiled.
Anyway, here's the code to leave out encapsulation characters:
<?php
function explode_quote($ADelim, $AString, $AEncap = '"') {
$retval = array();
//if we have an empty string, don't even bother processing
if (empty($AString))
return $retval;
//calculate source length and initialize some variables
$srcLength = strlen($AString);
$insideEncap = false;
//0..X string indexing in PHP, we'll be reading +1
$lastPos = -1;
for ($x=0;$x<$srcLength;$x++) {
if ($AString[$x]==$AEncap) {
if (!($insideEncap = !$insideEncap)) {
$retval[] = substr($AString, $lastPos+2, $x-$lastPos-2);
$lastPos = ++$x;
}
}
elseif (!$insideEncap && $AString[$x]==$ADelim) {
$retval[] = substr($AString, $lastPos+1, $x-$lastPos-1);
$lastPos = $x;
}
}
//if string doesn't end with delimiter, then throw remaining data to result
if ($lastPos!=$srcLength)
$retval[] = substr($AString, $lastPos+1, $srcLength);
return $retval;
}
?>