0

I have the code like this:

<div id="content">
...some included PHP files here
</div>

I need a solution in PHP like this:

if <div id="content"> contains more then 1 paragraph, 
then echo "more then one" after paragraph 1.

I need it in PHP not jQuery. Is it possible?

If it can't be done with paragraphs, it's also ok if the echo comes after 'x' number of words or 'y' number of characters.

There is a str_word_count function in PHP, but I have no idea how to get the content of a div that has PHP includes.

Shehary
  • 9,926
  • 10
  • 42
  • 71
10now
  • 387
  • 2
  • 3
  • 14
  • 3
    use the ob system. `ob_start(); ... build your html here ... ; $html = ob_get_clean()`, then feed that html to DOM and do your div/paragraph discovery. – Marc B Mar 12 '15 at 14:09
  • after getting your html in a variable as suggested by @MarcB you can use PHP DOMDocument object for html traversing – siddhesh Mar 12 '15 at 14:20

2 Answers2

1

You can use the output buffering.

Do something like:

ob_start();
?>
<div></div>
$html = ob_get_clean();

Then, count the number of paragraphs inside $html variable, and output or not your string.

blue112
  • 52,634
  • 3
  • 45
  • 54
1

to get the values from tags and put it in a variable you can try this - https://stackoverflow.com/a/17638209/4660348

$html = '<div class="coursesListed">
    <ul>
    <li><a href="#"><h3>Item one</h3></a></li>
    <li><a href="#"><h3>item two</h3></a></li>
    <li><a href="#"><h3>Item three</h3></a></li>            
    </ul>
    </div>';

$doc = new DOMDocument();
$doc->loadHTML($html);
$liList = $doc->getElementsByTagName('li');
$liValues = array();
foreach ($liList as $li) {
    $liValues[] = $li->nodeValue;
}

var_dump($liValues);

Credits to Manoj Yadav

Community
  • 1
  • 1
DarkZ3ro11
  • 128
  • 7