2

I'm working on a project where I need to get all the data from a HTML class.

<div class="sprofile">
    <a href="http://www.cibap.nl/profile/2127/"><b>Jeroen Rinzema</b></a>
    <br />
    <i>Reclame en Media</i>
    <br />
    Klas RMM21C
    <div class="istatus offline">
    Offline
    </div>                
</div>

This is the HTML that I need to get from the HTML page. But I don't know how I can select this part of the HTML document in PHP. The parts I need to use are: Jeroen Rinzema and Klas RMM21C

If you want to see the full HTML document can you do that here.

What I have tried so far is:

$url = "http://www.cibap.nl/profile/$username";
$html = file_get_html($url);
foreach($html->find('div.sprofile') as $article){
    $item['name'] = $article->find('div.sprofile a b', 0)->plaintext;
    $item['klas'] = $article->find('div.sprofile', 0)->plaintext;
}
echo'done';
rgettman
  • 176,041
  • 30
  • 275
  • 357
Ajeo
  • 73
  • 5
  • 11
  • but can i also select a class like (sprofile) with DOM Parser? – Ajeo Oct 09 '13 at 18:18
  • you can send this part via jquery to a php file i think both files must be in a same domain) – Pooya Estakhri Oct 09 '13 at 18:18
  • Both files are not in the same domain this is the HTML of a page on another server – Ajeo Oct 09 '13 at 18:19
  • @MeQube: Yes, that's easy. Use an XPath expression. Something like: `//*[contains(@class, 'sprofile')]` -- see [this](http://stackoverflow.com/a/6366390/1438393) answer. – Amal Murali Oct 09 '13 at 18:23
  • ok that is usefull so what i can do is selecting the class 'sprofile' and than grabbing the content 'Jeroen Rinzema' from – Ajeo Oct 09 '13 at 18:25

3 Answers3

1

Use the Symfony DomCrawler Component with the CssSelector Component. It provides a very natural syntax and allows you to select elements based on their class.

If PHP ain't a requirement, may I suggest using nodejs (with or without jQuery)?

nietonfir
  • 4,797
  • 6
  • 31
  • 43
  • the best would be php but if jQuery the only option is. Is it no problem – Ajeo Oct 09 '13 at 19:22
  • The thing with Symfony is that you select the text itself but in my situation is the text always different so i never know what is standing in the html file – Ajeo Oct 09 '13 at 19:23
  • @MeQube You use CSS classes in your example that can be matched with the CssSelector Component. If the DOM you're going to parse is always different, there's no way you can get the desired output automatically. – nietonfir Oct 09 '13 at 19:28
0

Do you wish to send the html content to a php page? For that you can wrap the content inside hidden form elements and POST the same.

If you wish to pick up the html content on this page itself, use javascript dom manipulation. You can go through this link toohttp://stackoverflow.com/questions/3808808/how-to-get-element-by-class-in-javascript

Ritesh A
  • 283
  • 1
  • 2
  • 12
-1

Try PHP Simple HTML DOM Parser

http://simplehtmldom.sourceforge.net/

though if you're doing this client side use jQuery.

logic-unit
  • 4,195
  • 12
  • 47
  • 72