PHP特殊模式
我使用PHP特殊模式,php,html,regex,Php,Html,Regex,我使用preg\u match\u all()从HTML文件中提取所有键字符串。 每一个键字符串都在一个模式中,如\\\$%&key\\\\\\\\\\\\\\\\\\\\\ 因此,我: $html = file_get_contents("htmlFile.html"); if ($html){ $matches = null; $keys = preg_match_all("/(_@#\$%&)(?P<key>\w+)(&%\$#@_)/", $html,
preg\u match\u all()
从HTML文件中提取所有键字符串。
每一个键字符串都在一个模式中,如\\\$%&key\\\\\\\\\\\\\\\\\\\\\
因此,我:
$html = file_get_contents("htmlFile.html");
if ($html){
$matches = null;
$keys = preg_match_all("/(_@#\$%&)(?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=文件获取内容(“htmlFile.html”);
如果($html){
$matches=null;
$keys=preg\u match\u all(“/(\\$%&)(?P\w+)(&%\$\\\\\\\\$)/”,$html,$matches,preg\u SET\u顺序);
如果(($keys>=0)和($keys!=false)){
如果($keys==0)
echo“preg_match_all()返回0”;
否则{
foreach($匹配为$val)
echo$val[键];
}
}
}
HTML文件内容:
<label for="button_ok">_@#$%&LABEL_BUTTON_OK&%$#@_</label>
<input type="button" value="_@#$%&TEXT_BUTTON_OK&%$#@_" />
。@$%&标签按钮\u确定&%$#@_
在测试时,它说没有找到模式。使用双反斜杠
\
引用$
$html = '<label for="button_ok">_@#$%&LABEL_BUTTON_OK&%$#@_</label>
<input type="button" value="_@#$%&TEXT_BUTTON_OK&%$#@_" />';
preg_match_all("/(_@#\\$%&)(?P<key>\w+)(&%\\$#@_)/", $html, $matches, PREG_SET_ORDER);
print_r($matches);
$html='\@$%&标签按钮\u确定&%$#@_
';
preg\u match\u all(“/(\\\$%&)(?P\w+)(&%\\$\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\)/”,$html,$matches,preg\u SET\u ORDER);
打印(匹配项);
尝试使用(“”)
单引号来匹配字符串
$html = '<label for="button_ok">_@#$%&LABEL_BUTTON_OK&%$#@_</label>
<input type="button" value="_@#$%&TEXT_BUTTON_OK&%$#@_" />';
preg_match_all('/(_@#\$%&)(?P<key>\w+)(&%\$#@_)/', $html, $matches, PREG_SET_ORDER);
//print_r($matches);
echo $matches[0][0]; //_@#$%&LABEL_BUTTON_OK&%$#@_
echo $matches[1][0]; //_@#$%&TEXT_BUTTON_OK&%$#@_
$html='\@$%&标签按钮\u确定&%$#@_
';
预匹配所有('/(\$%&)(?P\w+)(&%\$\\\\\\\\\\\\\\\\\\\$)/',$html,$matches,预设置顺序);
//打印(匹配项);
echo$匹配项[0][0];//$%&标签按钮正常&%$#@_
echo$匹配项[1][0];//$%&文本按钮(确定&%$#@)