Escaped explode/implode function I had a somewhat interesting issue.  I had to take an array and turn it into a string with a set of delimiters; then later on take that string and turn it back into an array, splitting at the same delimiters.  The twist was the string could be contain anything (including the delimiters).

I feel like I over-engineered the solution, and am wondering what you guys can come up with as alternatives.

Here is my solution.  escapedImplode turns an array into a string, and escapedExplode turns that string back into its corresponding array.  The testEscapedIE function is a little test scaffolding to see if your solution works.

function escapedImplode ($glue, $array, $escapeChar = '\\')
{
    $array = array_map(function ($item) use ($escapeChar, $glue) {
        $item = str_replace($escapeChar, $escapeChar . $escapeChar, $item);
        $item = str_replace($glue, $escapeChar . $glue, $item);

        return $item;
    }, $array);

    return implode($glue, $array);
}

function escapedExplode ($delimiter, $string, $escapeChar = '\\')
{
    $characters = str_split($string);

    $isEscaped = false;

    $parts = array();
    $buffer = '';

    for ($i = 0; $i &lt; count($characters); $i++)
    {
        $char = $characters[$i];

        // If is escaped, just add to the buffer and continue
        if ($isEscaped)
        {
            $buffer .= $char;
            $isEscaped = false;
            continue;
        }
        // If is a delimiter, which isn't escaped, set state and continue
        else if ($char == $escapeChar)
        {
            $isEscaped = true;
            continue;
        }

        // If not escaped and is the delimiter, break here
        if ($char == $delimiter)
        {
            $parts[] = $buffer;
            $buffer = '';
            continue;
        }

        // Doesn't match another special case, tack onto buffer
        $buffer .= $char;
    }

    // Add whatever is in the buffer to end of parts
    $parts[] = $buffer;

    return $parts;
}


function testEscapedIE ()
{
    $tests = array(
        array('test', 'cool', 'awesome'),
        array('some/thing', 'cool', '/isbrewing/'),
        array('with\\/asdf', '//other//', '\\/collness\\/'),
        array('////','//\\//','\\//\\//\\','\\\\'),
    );

    foreach ($tests as $test)
    {
        $imploded = escapedImplode('/', $test);
        $exploded = escapedExplode('/', $imploded);

        echo 'Testing: ' . implode(', ', $test) . ' -- ';
        echo $imploded . ' -- ';
        echo implode($exploded, ', ') . ': ';

        echo $test === $exploded ? '&lt;strong&gt;Pass&lt;/strong&gt;' : '&lt;strong&gt;Fail&lt;/strong&gt;';

        echo "&lt;br/&gt;\n";
    }
}

testEscapedIE();
Not bad.  I just have a few nitpicks.

You can iterate over characters of a string directly without splitting it into an array first:

for ($i = 0; $i &lt; strlen($string); $i++) {
    $char = $string[$i];
    ...
}


I believe that the PHP interpreter isn't smart enough to recognize that strlen($string) is invariant, so you might get better performance with

$strlen = strlen($string);
for ($i = 0; $i &lt; $strlen; $i++) {
    $char = $string[$i];
    ...
}


I find the else if in escapedExplode() slightly jarring.  Either use if and continue everywhere, or if, else if, else if, else without continue.