2

Hi there I am requesting you to tell me the simple php script to count special html tag, like <p> in string. I had no idea to try this, since I am the beginner in php.

1
  • 4
    "Gimme $5 and I'll do your homework." Seriously, this is a Q/A site, not a "gimme teh codez" do it for you site. Commented Feb 13, 2014 at 5:53

2 Answers 2

6

You can make use of DOMDocument Class for that .

<?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
Sign up to request clarification or add additional context in comments.

1 Comment

there is a very good answer here to read ALL of them: stackoverflow.com/questions/3184284/…
4

use a substr_count() function for find a mached substring from string:

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

    ?>

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.