2014-05-23 92 views
-1

我使用preg_match_all()爲了從HTML文件中提取所有密鑰字符串。 每個關鍵字串都在[email protected]#$%&KEY_NAME&%$#@_之類的模式中。PHP特殊模式

所以我必須:

$html = file_get_contents("htmlFile.html"); 
if ($html){ 
    $matches = null; 
    $keys = preg_match_all("/([email protected]#\$%&)(?P<key>\w+)(&%\$#@_)/", $html, $matches, PREG_SET_ORDER); 
    if (($keys >= 0)&&($keys != false)){ 
    if ($keys == 0) 
     echo "preg_match_all() returns 0"; 
    else{ 
     foreach($matches as $val) 
      echo $val[key]; 
    } 
    } 
} 

HTML文件內容:

<label for="button_ok">[email protected]#$%&LABEL_BUTTON_OK&%$#@_</label> 
<input type="button" value="[email protected]#$%&TEXT_BUTTON_OK&%$#@_" /> 

http://tryphpregex.com/測試它,它甾體抗炎藥,並非型樣。

回答

0

使用雙反斜線\\引述$

$html = '<label for="button_ok">[email protected]#$%&LABEL_BUTTON_OK&%$#@_</label> 
<input type="button" value="[email protected]#$%&TEXT_BUTTON_OK&%$#@_" />'; 
preg_match_all("/([email protected]#\\$%&)(?P<key>\w+)(&%\\$#@_)/", $html, $matches, PREG_SET_ORDER); 
print_r($matches); 
0

嘗試使用('')單引號匹配字符串

$html = '<label for="button_ok">[email protected]#$%&LABEL_BUTTON_OK&%$#@_</label> 
<input type="button" value="[email protected]#$%&TEXT_BUTTON_OK&%$#@_" />'; 
preg_match_all('/([email protected]#\$%&)(?P<key>\w+)(&%\$#@_)/', $html, $matches, PREG_SET_ORDER); 
//print_r($matches); 
echo $matches[0][0]; //[email protected]#$%&LABEL_BUTTON_OK&%$#@_ 
echo $matches[1][0]; //[email protected]#$%&TEXT_BUTTON_OK&%$#@_