regex - PHP - how to explode a string using a comma, except situtation when this comma is inside apostrophes? -
i have following text:
$string=' blah<br> @include (\'file_to_load\') <br> @include (\'file_to_load\',\'param1\',\'param2\',\'param3\') ';
i'd catch (and replace using preg_replace_callback) occurences of "@include" parameters (e.g. @include ('file_to_load','param1','param2','param3') )
so this:
$string=' blah<br> @include (\'file_to_load\') <br> @include (\'file_to_load\',\'param1\',\'param2\') '; $params=[]; $result = preg_replace_callback( '~@include \((,?.*?)\)~',//i catch @include, parenthesis , between them function ($matches) { echo '---iteration---'; $params=explode(',',$matches[1]);//exploding comma echo '<pre>'; var_dump($params); echo '</pre>'; return $matches[1]; }, $string );
and everything's fine until comma appears inside parameter, here:
$string=' blah<br> @include (\'file_to_load\') <br> @include (\'file_to_load\',\'param1,something\',[\'elem\'=>\'also, comma\']]) ';
here have comma inside "param1" param, now, after exploding explode() function doesn't work want.
i there way explode() (by using regular expression probably) string comma, not when comma inside apostrophes?
use following split:
,(?=([^']*'[^']*')*[^']*$)
use preg_split
since explode
not support regex:
code:
$params = preg_split(',(?=([^']*'[^']*')*[^']*$)',$matches[1]);
Comments
Post a Comment