2014-02-13 21 views
0

嗨,我要求您告訴我簡單的PHP腳本來計算特殊的html標記,如< p> in string。我不想嘗試這個,因爲我是PHP的初學者。通過php在字符串中計算html標記

+2

「給我$ 5和我會做你的功課。」說真的,這是一個Q/A網站,而不是一個「給我的代碼」爲你做的網站。 –

回答

1

您可以使用DOMDocument類。

<?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 
+1

哦謝謝代碼,它完美的工作 – Dipak

0

使用substr_count()函數找到字符串mached子:

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

    ?> 
+0

這也是有用的.. – Dipak