Reputation: 1609
I am trying to parse php variables in a string using regex. For instance,
$str = '$str1var ="str1"; $str2var = "str2"';
would give me an array ['str1var', 'str2var']
My regex pattern in php is: "/\$(.*?)=/s"
$pattern = "/\$(.*?)=/s";
preg_match_all($pattern, $str, $output_array);
but preg_match_all is returning 0 (no matches) What is wrong with my regex? I tested on a site like http://www.phpliveregex.com/ and it works fine, but my server returns 0;
My Server PHP version is 5.1.6
Upvotes: 3
Views: 564
Reputation: 1688
$pattern = '/\$(?P<variable>.*?)(?:\s|)=(?:\s|)"(?P<value>.*?)"/';
Returns an associative array with the value if you need it.
Upvotes: 0
Reputation: 91734
PHP seems to have problem with the double quotes surrounding your pattern, perhaps it is looking for a variable because of the $
sign.
Your code does not work in any php version, but with single quotes it works in all php versions, see the example:
$pattern = '/\$(.*?)=/s';
Upvotes: 3