How to properly escape, split, and unescape data?
<p>This is an interview type question....</p>

<p>Suppose that you create a simple Simple Object Notation Language...we will call it LON for Lean Object Notation.  It only works for rows and fields, similar to a mysql table.  Each row is separated by a || and each field is separate by a |...Like this...</p>

<pre><code>field 1 | field 2 || field 1 | field 2 || field 1 | field 2 ||
</code></pre>

<p>The code for creating this is pretty straight forward...here it is for one case in PHP: <code>Mark::fieldM = |</code> and <code>Mark::rowM = ||</code></p>

<pre><code>public static function row( $function, $array_2d )
{
    $iter = 0;
    foreach( $array_2d as $array_row )
    {
        if( $iter != 0 )  
        {
            Mark::$stringReturn = Mark::$stringReturn . Mark::rowM;
        }
        ++$iter;
        call_user_func( $function, $array_row ); 
    }
    return self::$stringReturn;
}


public static function fieldBookmark($array1D)
{
    $array1D = Mark::escape($array1D);
    Mark::$stringReturn = Mark::$stringReturn 
        . $array1D['domain'] 
        . Mark::fieldM 
        . $array1D['url']
        . Mark::fieldM 
        . $array1D['title'];
}
</code></pre>

<p>To escape it you use : </p>

<pre><code>$array_row[ $key ] = str_replace( array( '*', '|' ), array( '**','|*' ), $array_row[ $key ] );
</code></pre>

<p>which of course is applied to the content in the fields to make sure if the user uses the | sign it is not misinterpreted as a field marker.</p>

<p>* becomes ** and | becomes |*</p>

<p>To parse the data on the client side using JavaScript you use</p>

<pre><code>first_split = embeddedAml.split( /\|\|(?!\*)/ );
for( element_count = 0; element_count &lt; first_split.length; element_count++ )
{
    second_split = first_split[ element_count ].split( /\|(?!\*)/ ); 
    second_split = ViewH.unEscape( second_split );
    passed_function( second_split );
}
</code></pre>

<p>This allow you to split on || but not ||* for the rows</p>

<p>and to split on </p>

<p>| but not on |*</p>

<p>for the fields.</p>

<p>The unEscape() function then removes the special characters as such:</p>

<pre><code>unEscape: function ( second_split )
{
    var element;
    for( element in second_split )
    {
        second_split[element] = second_split[element].replace( '**', '*', 'g' );
        second_split[element] = second_split[element].replace( '|*', '|', 'g' );
    }
    return second_split;
},
</code></pre>

<p>This works on all cases except one.</p>

<p>If you make the very first character of user input a * it combines with the end of field marker | and it is now not properly recognized as a field but as an escaped special character.</p>

<p>For example</p>

<pre><code>some content in field 1 |*content in field 2 |* more in field 2 | field 3 ||
</code></pre>

<p>is broken because the of the first astricks....field 2 has now disappeared because the field marker | looks like a user entered special character |*</p>

<p>How do I properly escaped, split and unescape data? </p>

<p>Is it possible with this method does someone know the best practice way to escape data?</p>
