0

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.

1
  • 2
    This seems a lot like markdown, maybe have a look at a markdown parser? Commented Aug 22, 2013 at 15:56

3 Answers 3

1

You could maybe try this regex?

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

Demo on regex101.

Sign up to request clarification or add additional context in comments.

1 Comment

Awesome then @Deepsy! ^^
0

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
)

Comments

0

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
)

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.