Search code examples
phpdynamicforeachsimple-html-dom

dynamic foreach into foreach


I am trying to read a random website's HTML by using PHP Simple HTML DOM Parser

For example, I am trying to read a span on some website.

span position is in body > div[class=row] > span

I can use this method for catch the span target:

foreach ($html->find('body') as $element) {
    foreach ($html->find('div[class=row]') as $element) {
        foreach ($html->find('span') as $element) {

        }
    }
}

This code works fine. But what if I want to create a dynamic span position?

For exampe, The span position was body > div[class=row] > div[class=con] > span , How can I make it dynamic in foreach?

I have some dynamic input for entrance tags names like div or something. these inputs can be 1 or more, I can send theme by Form, but the problem is:

How to make enough foreach into the last one? like:

Foreach > Foreach > ...


Solution

  • Instead of arrow(>) you can use space( ),

    foreach ($html->find('body') as $element) {
        // Find all <span> in <div> which class=row 
        foreach ($html->find('div.row span') as $element) {
             // check span data here
        }
    }