如何使用php计算div中div的总数

I want to count total number of div within a specific div. How can I do this? For Example:

<div class="items">
   <div class="single_item">Item 1</div>
   <div class="single_item">Item 1</div>   
   <div class="single_itemitem">Item 1</div>
</div>

I want to count all the div containing class name single_item. How can I do this?

Use javascript and Jquery not php.

Use a script like this to do that:

 var count=0;
$( ".single_item" ).each(function() {

 count += 1;
 console.log(count);
 });

Look that jquery each() function

Salman, PHP runs on your server - it does not see or know anything about what is seen in the web browser. Whatever code you run on PHP stops BEFORE it gets to the browser.

The web browser is the client. Typically, the client will request a page from the server - the server generates the page (either static or dynamically using languages like PHP) and serve it back to the client.

When the page is received by the client, languages such as javascript will help you manipulate the data received. Languages like javascript will also allow you to manipulate the web page on the client.

I suggest you step back a bit - forget PHP for a moment - get a simple editor, even windows notepad if thats your thing, and create a simple html page. Once you've done that, write a simple javascript that prompts you to enter your name, and then displays "Hello " and the name that was entered.

Walk don't run. The hair on your head will thank you.

If you want to do this job with php, you can utilize php's native DOMDocument class. Try the following code, which will demonstrate the process:

<?php

$dom_document = new DOMDocument();

$dom_document->load("html.php");
$finder = new DomXPath($dom_document);
$classname = "single_item";
$results = $finder->query("//*[@class='" . $classname . "']");

echo $results->length;

in the above example your div lives in the html.php file.

The html.php file looks like below:

<div class="items">
   <div class="single_item">Item 1</div>
   <div class="single_item">Item 1</div>   
   <div class="single_itemitem">Item 1</div>
</div>