Solved

PHP Simple HTML DOM Parser - select div with multiple classes

Posted on 2012-12-21
10
2,897 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
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
 

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
Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

 
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 34

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

Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

Question has a verified solution.

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

Suggested Solutions

Consider the following scenario: You are working on a website and make something great - something that lets the server work with information submitted by your users. This could be anything, from a simple guestbook to a e-Money solution. But what…
Since pre-biblical times, humans have sought ways to keep secrets, and share the secrets selectively.  This article explores the ways PHP can be used to hide and encrypt information.
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…
The viewer will learn how to count occurrences of each item in an array.

947 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

21 Experts available now in Live!

Get 1:1 Help Now