Go Premium for a chance to win a PS4. Enter to Win

x
?
Solved

PHP Simple HTML DOM Parser - select div with multiple classes

Posted on 2012-12-21
10
Medium Priority
?
3,561 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 35

Expert Comment

by:gr8gonzo
ID: 38713443
Try:

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

or try:

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

Expert Comment

by:jkdt0077
ID: 38713508
If above don't work:

$html->find('div[class=.mada.rock.rolling]');
0
Industry Leaders: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

 

Author Comment

by:potworny
ID: 38714153
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
ID: 38715358
This here seams quite simple:

simple html parser
0
 
LVL 4

Expert Comment

by:brendonfeeley
ID: 38715366
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
ID: 38715396
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
ID: 38724821
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 35

Accepted Solution

by:
gr8gonzo earned 2000 total points
ID: 38733466
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
ID: 38737362
THANK you very much :)
0

Featured Post

What does it mean to be "Always On"?

Is your cloud always on? With an Always On cloud you won't have to worry about downtime for maintenance or software application code updates, ensuring that your bottom line isn't affected.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Introduction This article is intended for those who are new to PHP error handling (https://www.experts-exchange.com/articles/11769/And-by-the-way-I-am-New-to-PHP.html).  It addresses one of the most common problems that plague beginning PHP develop…
There are times when I have encountered the need to decompress a response from a PHP request. This is how it's done, but you must have control of the request and you can set the Accept-Encoding header.
Explain concepts important to validation of email addresses with regular expressions. Applies to most languages/tools that uses regular expressions. Consider email address RFCs: Look at HTML5 form input element (with type=email) regex pattern: T…
The viewer will learn how to create and use a small PHP class to apply a watermark to an image. This video shows the viewer the setup for the PHP watermark as well as important coding language. Continue to Part 2 to learn the core code used in creat…
Suggested Courses

916 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