我想检索数组中以html页面的$开头的所有单词。
HTML页面内容test.html
<p>{$Nom}</p>
<p>{$Prenom}</p>
<p>{$Adresse}</p>
<p>{$CP}</p>
<p>{$Ville}</p>
功能PHP
$filename = "test.html";
$handle = fopen($filename, "r");
$input = fread($handle, filesize($filename));
$pattern = '/[$](\w+)/';
$matches = array();
preg_match_all('/\$\w+/', $input, $matches);
print_r($matches);
结果显示
数组([0] =>数组([0] => $姓[1] => $姓[2] => $地址[3] => $ CP [4] => $ CITY [5] = >))
所需的重复结果:
姓氏名字地址CP CITY
实际上,有比完成Ray告诉您的方法更简单的方法。你可以
$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);
因为在Regex中用()包裹内容实际上会返回另一个包含匹配部分的数组。因此,将返回另一个包含字符串(\ 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]
本文收集自互联网,转载请注明来源。
如有侵权,请联系 [email protected] 删除。
我来说两句