2
<div style="float: left; margin-top: 10px; font-family: Verdana; font-size: 13px; color: #404040;">innertext</div>

Jow can I access innertext of divs not having class or id but span using simple html dom php parser? Thanks.

James Wiseman
  • 29,946
  • 17
  • 95
  • 158
cj333
  • 2,547
  • 20
  • 67
  • 110

4 Answers4

5

If the styles are consistent, then you can loop over all divs in the document and filter them by style.

var divs = document.getElementsById("div");

for (var i = 0; i < divs.length; i++) {
    var div = divs[i];

    // skip the current div if its styles are wrong
    if (div.style.cssFloat !== "left"
     || div.style.marginTop !== "10px"
     || div.style.fontFamily !== "Verdana"
     || div.style.fontSize !== "13px"
     || div.style.color !== "#404040") continue;

    var text = div.innerText || div.textContent;

    // do something with text
}
Ben Blank
  • 54,908
  • 28
  • 127
  • 156
3

You may use the content of style tag if no id or class is given there like:

include('simple_html_dom.php');
$html = file_get_html('http://www.mysite.com/');
foreach($html->find('div[style="float: left; margin-top: 10px; font-family: Verdana; font-size: 13px; color: #404040;"]') as $e)
echo $e->innertext;
agf
  • 171,228
  • 44
  • 289
  • 238
gurjeet kj
  • 99
  • 1
  • 1
0

You could probably try to match some of their parents (which have class or id set), then traverse the DOM to the child you want.

rdamborsky
  • 1,920
  • 1
  • 16
  • 21
0

Thanks to all. I depends on simple_html_dom too much, Ben Blank give me a good way. And I also tried php regular-expression to match the div by myself.

preg_match_all('/<div.*(style="float: left; margin-top: 10px; font-family: Verdana; font-size: 13px; color: #404040;").*>([\d\D]*)<\/div>/iU',$html,$match);
print_r($match); 
cj333
  • 2,547
  • 20
  • 67
  • 110