phparraystext-parsing

Parse square brace tagged text into an associative array


I have a php string that looks like this...

[line1]this is some test text[/line1][line2]This is line 2 text[/line2][line3]This is line 3 text[/line3]

I am trying to take this and create an array that looks like this..

array(
    "line1" => "this is some test text",
    "line2" => "This is line 2 text",
    "line3" => "This is line 3 text"
)

The string is dynamically created so it could consist of line1 - line 99 and so on.

What is the best way to do this and keep it scalable? Does anybody have an example they can point me at?


Solution

  • You may take this approach (by splitting the input string multiple times) with explode in order to isolate the relevant data: line-id and the line-text:

    $text = "[line1]this is some test text[/line1][line2]This is line 2 text[/line2][line3]This is line 3 text[/line3]";
    
    $text = explode( '][line', ']'.$text.'[line');
    
    // you'll get a spurious item at index 0 and at the end, they'll be skipped
    
    $n = count( $text );
    
    $output = array();
    
    for( $i = 1; $i < $n - 1; $i++ )
    {
        $line = explode( ']', $text[ $i] );
        $id = 'line' . $line[ 0 ];
        $line = explode( '[', $line[ 1 ] );
        $value = $line[ 0 ];
        $output[ $id ] = $value;
    }
    
    var_export( $output );
    echo "\n";
    

    You get:

    array (
      'line1' => 'this is some test text',
      'line2' => 'This is line 2 text',
      'line3' => 'This is line 3 text',
    )
    

    Note:

    Empty "lines" are tolerated and preperly handled

    A square brake inside the line text would break the code and screw everything up.

    The input format must be scrictly in the form

    [linen]text[/linen]....

    If you have further requirements you may adjust the code.

    I think this may be a good starting point.

    Note (again):

    This is a working solution (given the restictions mentinoned above).

    Another approach is to go with regular expressions and preg_match_all() using capturing groups to retrieve line id and line text.