preg_match_all - Extract key / value from template

Viewed 630

I am storing the following in a mysql database:

%URL% https://google.com
%TEXT% Hello world!
%LARGETEXT% Hello
My name is ...
I am from ...

My goal is to make the strings which are wrapped in % to PHP array keys and the string besides are the values.

The problem is, that my regex does not extract the multiline strings.

Here is the code:

preg_match_all ("/%(\w+)%(.*)/", $msg, $matches);

It does output:

  [1]=>
  array(3) {
    [0]=>
    string(5) "BASIC"
    [1]=>
    string(4) "TEXT"
    [2]=>
    string(9) "LARGETEXT"
  }
  [2]=>
  array(3) {
    [0]=>
    string(18) " https://google.de"
    [1]=>
    string(13) " Hello world!"
    [2]=>
    string(6) " Hello"
  }

At the second array only 'Hello' is shown, instead of:

Hello
My name is ...   
I am from ...

I tryed various regex but i come always to the same result.

2 Answers
Related