Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/php/238.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
计算PHP页面中的所有HTML标记_Php_Html_Regex - Fatal编程技术网

计算PHP页面中的所有HTML标记

计算PHP页面中的所有HTML标记,php,html,regex,Php,Html,Regex,我花时间在正则表达式上解决这个问题,但没有结果 我尝试使用PHP5.3解决这个问题 信息如-页面中重复的次数以及页面中所有标记的相关信息。我建议您签出简单html dom 不幸的是,你的问题目前的形式几乎无法理解。请尝试更新它,并更具体。如果要统计页面中的所有HTML标记,可以执行以下操作: $HTML = <<< HTML <html> <head> <title>Some Text</title>

我花时间在正则表达式上解决这个问题,但没有结果 我尝试使用PHP5.3解决这个问题
信息如-页面中重复的次数以及页面中所有标记的相关信息。

我建议您签出
简单html dom


不幸的是,你的问题目前的形式几乎无法理解。请尝试更新它,并更具体。如果要统计页面中的所有HTML标记,可以执行以下操作:

$HTML = <<< HTML
<html>
    <head>
        <title>Some Text</title>
    </head>
    <body>
        <p>Hello World<br/>
            <img src="earth.jpg" alt="picture of earth from space"/>
        <p>
        <p>Counting Elements is easy with DOM</p>
    </body>
</html>
HTML;
上面将输出
8
,因为DOM中有八个元素。如果您还需要了解元素的分布,可以这样做

$elementDistribution = array();
foreach($allElements as $element) {
    if(array_key_exists($element->tagName, $elementDistribution)) {
        $elementDistribution[$element->tagName] += 1;
    } else {
        $elementDistribution[$element->tagName] = 1;
    }
}
print_r($elementDistribution);
这会回来的

Array (
    [html] => 1
    [head] => 1
    [title] => 1
    [body] => 1
    [p] => 2
    [br] => 1
    [img] => 1
)
请注意,getElementsByTagName仅返回。它不考虑关闭标记,也不返回其他DOMNodes。如果还需要对结束标记和其他节点类型进行计数,请考虑使用。
这就是你想要的吗?

请发布你尝试过的内容。如果您只需要一个dom元素的计数,您可能会从javascript中获得更好的结果,但是php可以使用libxml来实现这一点。。。假设它是有效的xhtml。请看Gordon的回答:我尝试使用reg exp和substr逻辑来解决这个问题。下一步所有页面转换为一个长字符串,找到html标记,然后在删除所有内容并计算下一步后在文本中继续所有内容。这是一个用于一个简单任务的大程序包:/div-5 a-7 p-22 Maby DOMDocument等信息需求不是此任务的最佳解决方案?是的,谢谢这就是我这么长时间以来一直在折磨它的原因,非常好的回答,非常感谢
Array (
    [html] => 1
    [head] => 1
    [title] => 1
    [body] => 1
    [p] => 2
    [br] => 1
    [img] => 1
)
$testHTML = file_get_contents('index.html');

$search = preg_match_all('/<([^\/!][a-z1-9]*)/i',$testHTML,$matches);

echo '<pre>';
var_dump($matches[1]);
echo '</pre>';
echo '<pre>';
var_dump(array_count_values($matches[1]));
echo '</pre>';
array(5) {
  ["html"]=>
  int(1)
  ["head"]=>
  int(1)
  ["title"]=>
  int(1)
  ["body"]=>
  int(1)
  ["h1"]=>
  int(2)
}