Search code examples
phpregexpreg-match-allstring-parsing

preg_match_all extract from a text


I'm trying to use preg_match_all to extract an array from a text array format:

explanation: i've this kind of texte

mynicetext
text with other elements
|my first Column            |my second column   |
|my data in first column    |data 2             |    
|my data in first column    |data 3             |       
....   
other text
text with other elements
|my first Column            |my second column   | My last column |
|my data in first column    |data 2             |data 2          |     
|my data in first column    |data 3             |data 2          |
....

I want to extract a full array like this:

|my first Column            |my second column   |
|my data in first column    |data 2             |    
|my data in first column    |data 3             | 

I've tried to use this to ~(\s*\n)\|(.*)\|(\s*\n)~ to extract but i cut lines caused by the carriage return.

|my first Column            |my second column   |

then

|my data in first column    |data 2             | 

...

Any idea?


Solution

  • You can try this with preg_match_all:

    $pattern = '~(?:^\|.*\n)+~m';