2014-05-23 48 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&%$#@_