-1

I need to extract text from a long string that has extra text within square brackets:

[vc_row][vc_column width="1/1"]28/04/2015 Text description[vc_row][vc_column width="1/1"][vc_row][vc_column width="1/1"]29/04/2015 Text description 2[vc_row][vc_column width="1/1"]

To extract the text and exclude the content inside the square brackets i apply this:

$page_content = preg_replace("/\[([^\[\]]++|(?R))*+\]/", "", $page->post_content);

Until here, it works. Now i can output my result and i see my string printed within tags:

<p>28/04/2015</p><h3><a>description</a></h3>
<p>29/04/2015</p><h3><a>description 2</a></h3>

But i need to create an array of the text i'm extracting, like this:

Array ( 
     [0] => [28/04/2015] 
     [1] => [description]
     [2] => [29/04/2015] 
     [3] => [description 2] 
)

Or also

Array ( 
     [0] => Array (
         [0] => [28/04/2015] 
         [1] => [description]
     )
     [1] => Array (
         [0] => [29/04/2015] 
         [1] => [description 2]
     )
)

How to do this?

Luca Mormile
  • 1,175
  • 6
  • 19
  • 36

2 Answers2

0

Such regex help you

\](\d{1,2}\/\d{2}\/\d{4})\s([^\[]+)\[

MATCH 1
1.  [31-41] `28/04/2015`
2.  [42-58] `Text description`
MATCH 2
1.  [120-130]   `29/04/2015`
2.  [131-149]   `Text description 2`

There example

splash58
  • 25,715
  • 3
  • 20
  • 32
0

Branch reset \K can help here:

preg_match_all('~\[(?:[^][]++|(?R))*+\]\K(\d{2}/\d{2}/\d{4})\h+([^[]+)~', $s, $m);
unset ($m[0]);
print_r($m);

Array
(
    [1] => Array
        (
            [0] => 28/04/2015
            [1] => 29/04/2015
        )    
    [2] => Array
        (
            [0] => Text description
            [1] => Text description 2
        )    
)

RegEx Demo

anubhava
  • 713,503
  • 59
  • 514
  • 593