Solved

PHP Simple HTML DOM Parser - select div with multiple classes

Posted on 2012-12-21
10
3,294 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 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
WordPress Tutorial 2: Terminology

An important part of learning any new piece of software is understanding the terminology it uses. Thankfully WordPress uses fairly simple names for everything that make it easy to start using the software.

 

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

Secure Your WordPress Site: 5 Essential Approaches

WordPress is the web's most popular CMS, but its dominance also makes it a target for attackers. Our eBook will show you how to:

Prevent costly exploits of core and plugin vulnerabilities
Repel automated attacks
Lock down your dashboard, secure your code, and protect your users

Question has a verified solution.

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

Nothing in an HTTP request can be trusted, including HTTP headers and form data.  A form token is a tool that can be used to guard against request forgeries (CSRF).  This article shows an improved approach to form tokens, making it more difficult to…
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 dynamically set the form action using jQuery.

626 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