Solved

PHP Simple HTML DOM Parser - select div with multiple classes

Posted on 2012-12-21
10
3,104 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 4

Expert Comment

by:brendonfeeley
ID: 38713445
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 500 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

Free Tool: ZipGrep

ZipGrep is a utility that can list and search zip (.war, .ear, .jar, etc) archives for text patterns, without the need to extract the archive's contents.

One of a set of tools we're offering as a way to say thank you for being a part of the community.

Question has a verified solution.

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

Foreword (July, 2015) Since I first wrote this article, years ago, a great many more people have begun using the internet.  They are coming online from every part of the globe, learning, reading, shopping and spending money at an ever-increasing ra…
Password hashing is better than message digests or encryption, and you should be using it instead of message digests or encryption.  Find out why and how in this article, which supplements the original article on PHP Client Registration, Login, Logo…
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…

749 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