通过php在字符串中计算html标签

时间:2014-02-13 05:50:41

标签: php

您好我请求您告诉我简单的PHP脚本来计算特殊的html标记,例如< p>在字符串中。我不知道尝试这个,因为我是php的初学者。

2 个答案:

答案 0 :(得分:2)

使用substr_count()函数从字符串中查找mached子字符串:

  <?PHP
    $text = 'This is a test';
    //Use substr_count for search matched string
    echo substr_count($text, 'is'); // 2

    ?>

答案 1 :(得分:1)

您可以使用DOMDocument Class。

<?php
$html='<p>Hey hello</p><b>Hey this is bold tag</b><p>Another paragraph</p>';
$dom = new DOMDocument;
$dom->loadHTML($html);
echo $dom->getElementsByTagName('p')->length;// "prints" 2