Search code examples
phpregexsplitpreg-split

PHP Slipt Text and assign to an Array


I am trying to split and assign the values into an array for a text like this:

Title: Wonderful World

----

Text: 
Lorem ipsum dolor sit amet, consectetur adipiscing elit. 
Sed facilisis nulla dui, etiaculis enim porta aliquet. 
Etiam ante mauris, luctus non ultricies ut, pellentesque non eros. 

<b>Pellentesque</b> sit amet eros in quam pharetra fermentum quis ac lacus.
 Maecenas turpis purus, molestie eu quam non, adipiscing hendrerit nibh.

Go to <a href="/">Main Site</a>

----

Image: mysite.com/images/logo.png

After splitting and parsing it should be equivalent to PHP array like:

array (
    'Title' => "Wonderful World",
    'Text' => "Lorem ipsum dolor sit amet, consectetur adipiscing elit. 
              Sed facilisis nulla dui, etiaculis enim porta aliquet. 
              Etiam ante mauris, luctus non ultricies ut, pellentesque non eros. 

              <b>Pellentesque</b> sit amet eros in quam pharetra fermentum quis ac lacus.
               Maecenas turpis purus, molestie eu quam non, adipiscing hendrerit nibh.

              Go to <a href="http://google.com/>Google</a>",
    'Image' => "Image: mysite.com/images/logo.png"
);

So basically what it will do is:

  1. Split the text by 4 dashes ----, so it will ignore if it's less than or more than 4 dashes. If possible, also ignore 4 dashing following by other characters like

    ----xxxx
    

    while

    ---- xxxx
    

    should work (followed by a space or line break).

  2. Create array key for the first word followed by the first colon

  3. Create array value that comes after a word followed by column until it's end of file or meets another ----

  4. It should preserve the HTML tags and lines

  5. If there is only one keyword with colon even without 4 dash separator, it will still assigned to an array with sing element, so if the text contains:

    Title: Wonderful World

    will still create

    array (
        'Title' => "Wonderful World"
    );
    
  6. It should be intelligent enough to ignore the spaces between the keyword and colon, so the following 3 examples will be treated the same way:

    Title: Wonderful World

    Title :Wonderful World

    Title : Wonderful World

    and still able to create the array like

    array (
        'Title' => "Wonderful World"
    );
    

I have looked into YAML, but it's not ideal for standard text input. Do you know any PHP library or how I can pull this off? Thank you.


Solution

  • Try this:

    <?php
    $src = <<<END_SRC
    Title: Wonderful World
    
    ----
    
    Text: 
    Lorem ipsum dolor sit amet, consectetur adipiscing elit. 
    Sed facilisis nulla dui, etiaculis enim porta aliquet. 
    Etiam ante mauris, luctus non ultricies ut, pellentesque non eros. 
    
    <b>Pellentesque</b> sit amet eros in quam pharetra fermentum quis ac lacus.
     Maecenas turpis purus, molestie eu quam non, adipiscing hendrerit nibh.
    
    Go to <a href="/">Main Site</a>
    
    ----
    
    Image: mysite.com/images/logo.png
    END_SRC;
    
    $a = preg_split('/----\s/',$src);
    
    $data = array();
    foreach ($a as $part) {
        list ($key,$value) = explode(':',$part,2);
        $key = trim($key);
        $value = trim($value);
        if (isset($data[$key])) $data[$key] .= "\n\n$value";
        else $data[$key] = $value;
    }
    
    print_r($data);
    
    ?>