hellomello
hellomello

Reputation: 8597

cURL + PHP to getting a class name, possible?

Is it possible to get a certain class name through cURL?

For instance, I just want to get the 123 in this <div class="123 photo"></div>.

Is that possible?

Thanks for your responses!

Edit:

If this is whats seen on a website

<div class="444 photo">...</div>
<div class="123 photo">...</div>
<div class="141 photo">...</div>

etc...

I'm trying to get all the numbers of this class, and putting in an some array.

Upvotes: 1

Views: 4148

Answers (1)

Morgon
Morgon

Reputation: 3319

cURL is only half the solution. Its job is simply to retrieve the content. Once you have that content, then you can do string or structure manipulation. There are some text functions you could use, but it seems like you're looking for something specific among this content, so you may need something more robust.

Therefore, for this HTML content, I'd suggest researching DOMDocument, as it will structure your content into an XML-like hierarchy, but is more forgiving of the looser nature of HTML markup.

$ch = curl_init();
// [Snip cURL setup functions]
$content = curl_exec($ch);

$dom = new DOMDocument();
@$dom->loadHTML($content); // We use @ here to suppress a bunch of parsing errors that we shouldn't need to care about too much.

$divs = $dom->getElementsByTagName('div');
foreach ($divs as $div) {
    if (strpos($div->getAttribute('class'), 'photo') !== false) {
      // Now we know that our current $div is a photo
      $targetValue = explode(' ', $dom->getAttribute('class'));

      // $targetValue will now be an array with each class definition.
      // What is done with this information was not part of the question,
      //   so the rest is an exercise to the poster.
    }
}

Upvotes: 4

Related Questions