How to count the number of p tags in a div?

<div class="wrap">
<p></p>
<p></p>
<p></p>
</div>

<div class="wrap_2">
<p></p>
<p></p>
</div>
March 19th 20 at 09:10
4 answers
March 19th 20 at 09:12
Solution
echo preg_match_all('#<div>(<p>.*</p>)</div>#isU', $text);

Return values ΒΆ
Returns the number of found occurrences of the pattern (which may be zero), or FALSE if during the execution any error.
https://www.php.net/preg_match_all
$text='<div><p></p><p></p></div>'; // 1 - jedediah.Schulist33 commented on March 19th 20 at 09:15
@jedediah.Schulist33, well, this is an example. It is necessary to choose modifiers, can U extra
Generally there probably should just paragraphs highlight without div - ivah_Zulauf commented on March 19th 20 at 09:18
I don't think.
preg_match_all searches for all matches to the pattern and does not involve nested searches. So that will have to be divided into two parts - first, find the div, and then second lookup all p - jedediah.Schulist33 commented on March 19th 20 at 09:21
@jedediah.Schulist33, Yes,no,
(<p>.*</p>)
should work - ivah_Zulauf commented on March 19th 20 at 09:24
Well, I googled php gaining online and test at any link.
Again, how preg_match_all works: it looks for a match and finds the substring of <div>.....</div>and this is the only occurrence. That is if you remove the div and /div, leaving only the <p>.*</p> U will find 2. And only 1. - jedediah.Schulist33 commented on March 19th 20 at 09:27
March 19th 20 at 09:14
Solution
document.body.querySelectorAll('.wrap p').length; // 3
document.body.querySelectorAll('.wrap_2 p').length; // 2
document.body.querySelectorAll('.wrap p, .wrap_2 p').length; // 5
a - mauricio.Ferry commented on March 19th 20 at 09:17
wait, you have to find all p tags that wrap within the class and not in any other.
<div class="wrap">
<p></p>
<p></p>
<p></p>
<p></p>

 <div class="not">
<p></p>
</div>

<p></p>
<p></p>
</div>

Your code prints here 7, need and 6. - kelly52 commented on March 19th 20 at 09:20
@kelly52,
document.body.querySelectorAll('.wrap > p').length; - Henriette commented on March 19th 20 at 09:23
Thank you) - kelly52 commented on March 19th 20 at 09:26
March 19th 20 at 09:16
Solution
(document.getElementsByClassName('wrap')[0].innerHTML.match(/<p[\s>]/gi)||[]).length;
and on php? - kelly52 commented on March 19th 20 at 09:19
Also :)
<?php
$html = '<p><p>
<p><p>';
echo preg_match_all('/<p[\s>]/i',$html); //4
?>
- jedediah.Schulist33 commented on March 19th 20 at 09:22
@jedediah.Schulist33Can here more specific:
$string = '
<div class="wrap">
<p></p>
<p></p>

 <div class="not">
<p></p>
</div>

<p></p>
</div>

<div class="wrap_2">
<p></p>
<p></p>
</div>';

echo preg_match_all('/<p[\s>]/i',$string);

In a regular expression to specify what we need to calculate the p tags inside diva class wrap - kelly52 commented on March 19th 20 at 09:25
Thanks in advance) - kelly52 commented on March 19th 20 at 09:28
This is more difficult. Very different from your original question.
spoiler
<?php
$string = '
<div class="wrap">
<p></p>
<p></p>

 <div class="not">
<p></p>
</div>

<p></p>
</div>

<div class="wrap_2">
<p></p>
<p></p>
</div>';

$startPos = 0; //Next position opening or closing div tag
$deep = 0; //Depth of nesting. More 0 we are not interested.

while (true) {
 $pos1 = strpos($string, '<div', $startPos);
 $pos2 = strpos($string, '</div>', $startPos);
 $mode = $pos1===false?
($pos2===false?0:-1)
:($pos2===false?1:($pos1<$pos2?1:-1));
 $startPos = $mode===1?$pos1+1:$pos2+1;
 if ($mode===0) break;
 elseif ($mode===1) {
 if($deep===0) $saveStart = $pos1;
$deep++;
 } else { //$mode==-1
$deep--;
 if ($deep!==0) continue;
 $div = substr($string, $saveStart, $startPos - $saveStart);
 echo 'div:' . preg_match_all('/<p[\s>]/i',$div) . "<br>\n";
}
}
?>

Conclusion:
div: 4<br>
div: 2<br>

- jedediah.Schulist33 commented on March 19th 20 at 09:31
March 19th 20 at 09:18
in one or all?
document.querySelectorAll('.wrap p').length
document.querySelectorAll('div p').length

Find more questions by tags PHP