如何使用php函数获取HTML页面中以{$开头的所有单词
我想在一个数组中检索html页面中以$开头的所有单词 HTML页面内容测试.HTML如何使用php函数获取HTML页面中以{$开头的所有单词,html,Html,我想在一个数组中检索html页面中以$开头的所有单词 HTML页面内容测试.HTML <p>{$Nom}</p> <p>{$Prenom}</p> <p>{$Adresse}</p> <p>{$CP}</p> <p>{$Ville}</p> 显示的结果 数组([0]=>Array([0]=>$NOM[1]=>$PRENOM[2]=>$ADRESSE[3]=>$CP[4]=>$
<p>{$Nom}</p>
<p>{$Prenom}</p>
<p>{$Adresse}</p>
<p>{$CP}</p>
<p>{$Ville}</p>
显示的结果
数组([0]=>Array([0]=>$NOM[1]=>$PRENOM[2]=>$ADRESSE[3]=>$CP[4]=>$VILLE[5]=>)
预期的重复结果:
名字
普雷诺姆
阿迪斯
人物配对关系
VILLE当然可以,只需执行以下操作,因为您需要阵列中的它们
<?php
$filename = "test.html";
$handle = fopen($filename, "r");
$input = fread($handle, filesize($filename));
$pattern = '/[$](\w+)/';
$matches = array();
preg_match_all('/\$\w+/', $input, $matches);
$final_result = array();
$index=0;
foreach($matches as $match){
$final_result[$index] = str_replace('\$','',$match[$index]);
$index++;
}
实际上有一种比照雷说的做更简单的方法。你可以
$filename = "test.html";
$handle = fopen($filename, "r");
$input = fread($handle, filesize($filename));
$matches = array();
// see the important part is wrapping the \w+ inside ( )
preg_match_all('/\$(\w+)/', $input, $matches);
print_r($matches);
因为在正则表达式中用()包装某些内容实际上会返回另一个包含匹配部分的数组。因此,将返回另一个数组,其中包含字符串的(\w+)部分,仅表示单词。因此,现在将输出上述内容
Array
(
[0] => Array
(
[0] => $Nom
[1] => $Prenom
[2] => $Adresse
[3] => $CP
[4] => $Ville
)
[1] => Array
(
[0] => Nom
[1] => Prenom
[2] => Adresse
[3] => CP
[4] => Ville
)
)
但别忘了,您现在需要$matches[1]
这正是我要找的。现在要复制,我必须使用array_unique?谢谢复制?您的意思是删除重复项,对吗?是的。如果要删除每个重复值,请在$matches[1]上使用array unique如果你能把我的答案记为正确答案,我将不胜感激
Array
(
[0] => Array
(
[0] => $Nom
[1] => $Prenom
[2] => $Adresse
[3] => $CP
[4] => $Ville
)
[1] => Array
(
[0] => Nom
[1] => Prenom
[2] => Adresse
[3] => CP
[4] => Ville
)
)