Deepsy
Deepsy

Reputation: 3800

Regular expression for spliting string to array

I got a string like this:

== Paragraph == 
=== title 1 ===
content

=== Title 2 ===
other content 

== Paragraph 2 ==
=== asd1 ===
dfdsfdsfdsfdsfdsfsdfdsf

=== asd2 ===
fgdfgfdgfdgfdgfdgfdgfdg

I'm trying to parse it into array like this:

$arr = array( 
    array("title"=>"Paragraph", "content" => "=== title 1 ===
content

=== Title 2 ===
other content "),
array("title"=>"Paragraph 2", "content" => "=== asd1 ===
dfdsfdsfdsfdsfdsfsdfdsf

=== asd2 ===
fgdfgfdgfdgfdgfdgfdgfdg"));

I've tried this expession for getting paragraph names:

preg_match_all("@== (.*?) ==.*?@is", $data, $paragraphs);

but this expresion isn't working as I want, because it doesn't match the whole paragraph the output is like this and it doesn't get the contents:

Array
    (
        [0] => Paragraph
        [1] => title 1
        [2] => Title 2
        [3] => Paragraph 2
        [4] => asd1
        [5] => asd2
    )

I have also tried ( nl2br cuz I don't know how to use new line in regex )

$data = nl2br($data);
preg_match_all("@== (.*?) ==<br />.*?@is", $data, $paragraphs);

but the result is something like this:

[0] => Array
    (
        [0] => == Paragraph == <br />
=== title 1 ===<br />
content<br />
<br />
 === Title 2 ===<br />
 other content <br />
 <br />
 == Paragraph 2 ==<br />
    )

I'm not understanding the regular expresions good and I can't figure out how to solve my problem.

Upvotes: 0

Views: 117

Answers (3)

AbsoluteƵER&#216;
AbsoluteƵER&#216;

Reputation: 7870

Here we're breaking down each of the elements into its own piece of the array using preg_match_all and look around assertions in RegEx.

<?php

    $pattern = "~((?<!=)(?<=={2})\s*((?<=\s)[^=]+(?=\s))\s*(?=={2})(?!=)|(?<====)\s*((?<=\s)[^=]+(?=\s))\s*(?====)|(?<==\s)\s*((?<=\s)[^=]+)\s*(?!=))~";

    $string="== Paragraph == 
    === title 1 ===
    content

    === Title 2 ===
    other content 

    == Paragraph 2 ==
    === asd1 ===
    dfdsfdsfdsfdsfdsfsdfdsf

    === asd2 ===
    fgdfgfdgfdgfdgfdgfdgfdg";

    preg_match_all($pattern,$string,$matches);


    print_r($matches[0]);


?>

Output

Array
(
    [0] => Paragraph
    [1] =>  title 1 
    [2] => 
content


    [3] =>  Title 2 
    [4] => other content 

    [5] => Paragraph 2
    [6] =>  asd1 
    [7] => 
dfdsfdsfdsfdsfdsfsdfdsf


    [8] =>  asd2 
    [9] => fgdfgfdgfdgfdgfdgfdgfdg
)

Upvotes: 0

Toto
Toto

Reputation: 91385

How about:

$str = '== Paragraph == 
=== title 1 ===
content

=== Title 2 ===
other content 

== Paragraph 2 ==
=== asd1 ===
dfdsfdsfdsfdsfdsfsdfdsf

=== asd2 ===
fgdfgfdgfdgfdgfdgfdgfdg';
$list = preg_split('/(?<!=)== (.+?) ==(?!=)/', $str, -1, PREG_SPLIT_DELIM_CAPTURE);
print_r($list);

output:

Array
(
    [0] =>
    [1] => Paragraph
    [2] =>
=== title 1 ===
content

=== Title 2 ===
other content


    [3] => Paragraph 2
    [4] =>
=== asd1 ===
dfdsfdsfdsfdsfdsfsdfdsf

=== asd2 ===
fgdfgfdgfdgfdgfdgfdgfdg
)

Upvotes: 0

Jerry
Jerry

Reputation: 71538

You could maybe try this regex?

@(==[^=]+==)(.+?(?=[^=]==[^=]|$))@s

Demo on regex101.

Upvotes: 1

Related Questions