2013-12-11 20 views
0

我要像存在於註釋Regualar則表達式來提取註釋參數

我已經做了提取參數這麼遠

$str = "(action=bla,arg=2,test=15,op)"; 
preg_match_all('/([^\(,]+)=([^,\)]*)/', $str, $m); 

$data = array_combine($m[1], $m[2]); 
var_dump($data); 

這給了以下出來把

array (size=3) 
    'action' => string 'bla' (length=3) 
    'arg' => string '2' (length=1) 
    'test' => string '15' (length=2) 

這是無視運(但我希望它有空值或空值)

但我想改善是,因此它可以在單引號內這種情況下值提取這些也

  • (動作=「VAL」,ABC)將分配給動作
  • (動作=「VAL」,ABC)相同,但它上面的也雙引號之間抽取值
  • (動作= [123,11,23])現在動作動作將包含陣列123,11,23(在此還需要有或不帶提取)

我不不想要完整的解決方案(如果你能做到,那麼最受歡迎)但我至少需要兩個

編輯

(編輯按與r3mus disucssion) 輸出應該像

array (size=3) 
    'action' => string 'bla' (length=3) 
    'arg' => string '2' (length=1) 
    'test' => string '15' (length=2) 
    'op' => NULL 

回答

1

編輯:

這最終成爲了很多不僅僅是一個簡單的更復雜正則表達式。它結束了尋找(第一道)是這樣的:

function validate($str) 
{ 
    if (preg_match('/=\[(.*)\]/', $str, $m)) 
    { 
     $newstr = preg_replace("/,/", "+", $m[1]); 
     $str = preg_replace("/".$m[1]."/", $newstr, $str); 
    } 
    preg_match('/\((.*)\)/', $str, $m); 
    $array = explode(",", $m[1]); 
    $output = array(); 
    foreach ($array as $value) 
    { 
     $pair = explode("=", $value); 
     if (preg_match('/\[(.*)\]/', $pair[1])) 
      $pair[1] = explode("+", $pair[1]); 
     $output[$pair[0]] = $pair[1]; 
    } 
    if (!isset($output['op'])) 
     return $output; 
    else 
     return false; 
} 

print_r(validate("(action=[123,11,23],arg=2,test=15)")); 

舊的東西,是不充分的:

如何:

([^\(,]+)=(\[.*\]|['"]?(\w*)['"]?)

工作示例/沙:http://regex101.com/r/bZ8qE6

或者如果您只需要捕獲內的數組:

([^\(,]+)=(\[(.*)\]|['"]?(\w*)['"]?)

+0

不錯,但這不是提取'op'如告訴 –

+0

好吧,掛在..在這個例子中,op被忽略。如果',op'不存在,你只希望它匹配嗎?或者只有'test = 15'是最後一個對象?你能詳細說明這個要求嗎? – brandonscript

+0

它可以出現在任何地方 –

0

您可以使用此代碼:

<pre><?php 
$subject = '(action=bla,arg=2,test=15,op, arg2=[1,2,3],arg3 = "to\\"t,o\\\\", ' 
     . 'arg4 = \'titi\',arg5=) blah=312'; 

$pattern = <<<'LOD' 
~ 
(?: \(\s* | \G(?<!^)) # a parenthesis or contiguous to a precedent match 
(?<param> \w+) 
(?: \s* = \s* 
    (?| (?<value> \[ [^]]* ])      # array 
     | "(?<value> (?> [^"\\]++ | \\{2} | \\.)*)" # double quotes 
     | '(?<value> (?> [^'\\]++ | \\{2} | \\.)*)' # single quotes 
     | (?<value> [^\s,)]++)      # other value 
    )? # the value can be empty 
)?  # the parameter can have no value 
\s* 
(?: 
    , \s*     # a comma 
    |       # OR 
    (?= (?<control> \))) # followed by the closing parenthesis 
) 
~xs 
LOD; 
preg_match_all($pattern, $subject, $matches, PREG_SET_ORDER); 

foreach($matches as $match) { 
    printf("<br>%s\t%s", $match['param'], $match['value']); 
    if (isset($match['control'])) echo '<br><br>#closing parenthesis#'; 
} 
?></pre> 
1

我知道它的回答,但你可以做到這一點,我認爲這是你想要的東西:

$str = '(action=bla,arg=2,test=15,op)'; 
preg_match_all('/([^=,()]+)(?:=([^,)]+))?/', $str, $m); 
$data = array_combine($m[1], $m[2]); 
echo '<pre>' . print_r($data, true) . '</pre>'; 

產出

Array 
(
    [action] => bla 
    [arg] => 2 
    [test] => 15 
    [op] => 
) 
+0

好,但這不是提取數組值[1,2,3]。嘗試把行動= [1,2,3]。但對於你我投票的東西 –