Solved

PHP Simple HTML DOM Parser - select div with multiple classes

Posted on 2012-12-21
10
2,858 Views
Last Modified: 2013-01-02
Hi,

I have a div:

<div class="mada rock rolling">Amateur question</div>

Open in new window


I want to find this div with the PHP Simple HTML DOM Parser.

The following code does not work:
$html->find('div.mada.rock.rolling');

Open in new window


Can you help me, brothers and sisters? :)
0
Comment
Question by:potworny
  • 3
  • 3
  • 2
  • +2
10 Comments
 
LVL 34

Expert Comment

by:gr8gonzo
Comment Utility
Try:

$html->find('div[class=mada rock rolling]');

or try:

$html->find('div.mada .rock .rolling');
0
 
LVL 4

Expert Comment

by:brendonfeeley
Comment Utility
0
 
LVL 7

Expert Comment

by:jkdt0077
Comment Utility
If above don't work:

$html->find('div[class=.mada.rock.rolling]');
0
 

Author Comment

by:potworny
Comment Utility
brendonfeeley, I am looking at your answer :)

gr8gonzo, jkdt0077, this does not work:

$html->find("div[class='mada rock rolling']");
$html->find("div[class='mada .rock .rolling']")
$html->find('div[class=.mada.rock.rolling]');
$html->find('div.mada .rock .rolling');
0
 
LVL 11

Expert Comment

by:mcnute
Comment Utility
This here seams quite simple:

simple html parser
0
Threat Intelligence Starter Resources

Integrating threat intelligence can be challenging, and not all companies are ready. These resources can help you build awareness and prepare for defense.

 
LVL 4

Expert Comment

by:brendonfeeley
Comment Utility
What about these:

$html->find('div.mada, div.rock, div.rolling');

$html->find('div[class=mada], div[class=rock], div[class=rolling]');
0
 
LVL 4

Expert Comment

by:brendonfeeley
Comment Utility
Also, and forgive me if you know this, but:

$html->find('div.mada, div.rock, div.rolling');

Will return an array of matches. In order to output all matches, you will need to loop through them:

foreach($html->find('div.mada, div.rock, div.rolling') as $element) {
    echo $element->plaintext . '<br/>';
}
0
 

Author Comment

by:potworny
Comment Utility
I don't think this lib can parse more than one class name...

I just load the HTML into a variable, replace the three classes with one name of my own and parse the modififed version of the HTML. :)
0
 
LVL 34

Accepted Solution

by:
gr8gonzo earned 500 total points
Comment Utility
Sorry - I was away on vacation but just got back and checking on this. It looks like you didn't use my code.

I suggested:
$html->find('div[class=mada rock rolling]');

You said you tried:
$html->find("div[class='mada rock rolling']");

Notice that I did not have the single quotes inside mine. I just tried my suggestion and it worked.
0
 

Author Closing Comment

by:potworny
Comment Utility
THANK you very much :)
0

Featured Post

IT, Stop Being Called Into Every Meeting

Highfive is so simple that setting up every meeting room takes just minutes and every employee will be able to start or join a call from any room with ease. Never be called into a meeting just to get it started again. This is how video conferencing should work!

Join & Write a Comment

Suggested Solutions

Title # Comments Views Activity
Responsive Design Tools 6 36
Adding Extra Information box 4 24
php documentation 4 19
Not needed 13 53
Introduction Many web sites contain image galleries; a common design for these galleries includes a page with a collection of thumbnail images.  You can click on each of the thumbnail images to see the larger version of the image.  This is easily i…
Part of the Global Positioning System A geocode (https://developers.google.com/maps/documentation/geocoding/) is the major subset of a GPS coordinate (http://en.wikipedia.org/wiki/Global_Positioning_System), the other parts being the altitude and t…
Learn how to match and substitute tagged data using PHP regular expressions. Demonstrated on Windows 7, but also applies to other operating systems. Demonstrated technique applies to PHP (all versions) and Firefox, but very similar techniques will w…
This tutorial will teach you the core code needed to finalize the addition of a watermark to your image. The viewer will use a small PHP class to learn and create a watermark.

744 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

11 Experts available now in Live!

Get 1:1 Help Now