统计页面PHP中的所有HTML标记 [英] Count all HTML tags in page PHP

查看:88
本文介绍了统计页面PHP中的所有HTML标记的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我花时间在正则表达式来解决这个问题,但没有结果
i尝试使用PHP 5.3
来解决这个问题,例如 - 页面中重复了多少次以及页面中所有标记的信息。不幸的是,你的问题在目前的形式中很难理解。请尝试更新它,并更具体。如果您想统计网页中的所有HTML代码,您可以执行以下操作:

  $ HTML = <<< HTML 
< html>
< head>
< title>一些文字< / title>
< / head>
< body>
< p> Hello World< br />
< p>
< p>使用DOM轻松计数元素< / p>
< / body>
< / html>
HTML;

用DOM计算所有DOME元素:

  $ dom = new DOMDocument; 
$ dom-> loadHTML($ HTML);
$ allElements = $ dom-> getElementsByTagName('*');
echo $ allElements->长度;

以上将输出 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);

这将返回

 数组(
[html] => 1
[head] => 1
[title] => 1
[body] => ; 1
[p] => 2
= 1
[img] => 1

请注意,getElementsByTagName返回 DOMElements 。它不考虑结束标记,也不返回其他DOMNode。如果您还需要计算结束标记和其他节点类型,请考虑使用 XMLReader 代替。


I spent time on regex to solve this problem but not have result i try solve this problem using PHP 5.3 Information like - How many times repeats in page and information about all tags in page.

解决方案

Your question is unfortunately barely understandable in it's current form. Please try to update it and be more specific. If you want to count all HTML tags in a page, you can do:

$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;

Counting all DOMElements with DOM:

$dom = new DOMDocument;
$dom->loadHTML($HTML);
$allElements = $dom->getElementsByTagName('*');
echo $allElements->length;

The above will output 8, because there is eight elements in the DOM. If you also need to know the distribution of elements, you can do

$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);

This would return

Array (
    [html] => 1
    [head] => 1
    [title] => 1
    [body] => 1
    [p] => 2
    [br] => 1
    [img] => 1
)

Note that getElementsByTagName returns DOMElements only. It does not take into account closing tags, nor does it return other DOMNodes. If you also need to count closing tags and other node types, consider using XMLReader instead.

这篇关于统计页面PHP中的所有HTML标记的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆