2014-11-21 152 views
1
fontSize=16.0, fontFamily=sans, align=0, color=FF0000, text="foo, bar" 

和我需要匹配爲吐。輸出將被正則表達式匹配分裂

array(
    'fontSize'=>'16.0', 
    'fontFamily'=>'sans', 
    'align'=>'0', 
    'color'=>'FF0000', 
    'text'=>'foo, bar' 
); 

我想未來,但它是壞的:

preg_spit("~[\s]="?[\s]"?,~", $string); 
+0

'preg_spit( 「〜[\ S] = \」[\ S] \ 「?〜」,$弦);' – 2014-11-21 08:37:53

+0

你不能用'分裂,',因爲,在''foo,bar「' – 2014-11-21 08:38:49

回答

0

根據下面的正則表達式只是分割你輸入的字符串,

,\s(?![^=]*") 

DEMO

<?php 
$str = 'fontSize=16.0, fontFamily=sans, align=0, color=FF0000, text="foo, bar"'; 
$regex = '~,\s(?![^=]*")~'; 
$splits = preg_split($regex, $str); 
print_r($splits); 
?> 

輸出:

Array 
(
    [0] => fontSize=16.0 
    [1] => fontFamily=sans 
    [2] => align=0 
    [3] => color=FF0000 
    [4] => text="foo, bar" 
) 

正則表達式:

,      ',' 
\s      whitespace (\n, \r, \t, \f, and " ") 
(?!      look ahead to see if there is not: 
    [^=]*     any character except: '=' (0 or more 
          times) 
    "      '"' 
)      end of look-ahead 
+0

這就是它!問候 – 2014-11-21 08:44:49