3v4l.org

run code in 200+ php & hhvm versions
Bugs & Features
<?php $keywordRegex = '(?<key>-?mock)'; $valueRegex = '"(?<quoted>(?:[^"]|\\\")*)"|(?<unquoted>[^"\s]+)'; $regex = "/(?<=^|\\s){$keywordRegex}:\\s*{$valueRegex}/"; echo $regex, "\n\n"; $inputs = array( 'mock:"foo"', 'mock:bar\"foo', 'mock:""foo""' ); foreach ( $inputs as $term ) { $matches = null; echo $term, "\n"; preg_match( $regex, $term, $matches ); var_dump( $matches ); }
based on KPORh
Output for 5.6.0 - 7.3.0beta1
/(?<=^|\s)(?<key>-?mock):\s*"(?<quoted>(?:[^"]|\\")*)"|(?<unquoted>[^"\s]+)/ mock:"foo" array(5) { [0]=> string(10) "mock:"foo"" ["key"]=> string(4) "mock" [1]=> string(4) "mock" ["quoted"]=> string(3) "foo" [2]=> string(3) "foo" } mock:bar\"foo array(7) { [0]=> string(9) "mock:bar\" ["key"]=> string(0) "" [1]=> string(0) "" ["quoted"]=> string(0) "" [2]=> string(0) "" ["unquoted"]=> string(9) "mock:bar\" [3]=> string(9) "mock:bar\" } mock:""foo"" array(5) { [0]=> string(7) "mock:""" ["key"]=> string(4) "mock" [1]=> string(4) "mock" ["quoted"]=> string(0) "" [2]=> string(0) "" }