Take the 2-minute tour ×
Stack Overflow is a question and answer site for professional and enthusiast programmers. It's 100% free, no registration required.
<div>A/C:front<span style="color:red;margin:8px">/
</span>Anti-Lock Brakes<span style="color:red;margin:8px">/
</span>Passenger Airbag<span style="color:red;margin:8px">/
</span>Power Mirrors<span style="color:red;margin:8px">/
</span>Power Steering<span style="color:red;margin:8px">/
</span>Power Windows<span style="color:red;margin:8px">/
</span>Driver Airbag<span style="color:red;margin:8px">/
</span>No Accidents<span style="color:red;margin:8px">/
</span>Power Door Locks<span style="color:red;margin:8px">/</span>
</div>

Appears like this on website :

A/C:front/Anti-Lock Brakes/Passenger Airbag/Power Mirrors/Power Steering/Power Windows/Driver Airbag/No Accidents/Power Door Locks/

I used $content = file_get_contents('url'); and now i need to shift through the data.

I need to fetch each one of the options above and put them in an array or something like :

$option = ("A/C:front","Anti-Lock Brakes","Passenger Airbag",....);

Any idea how to do this using php ?

share|improve this question
    
Provide full website contents or at least its URL. You might want to use DOM+XPath here. –  Paulo Freitas Nov 13 '13 at 19:32
    
Maybe it will be easier if you use 'simplehtmldom' or something similar. –  Nikitas Nov 13 '13 at 19:34
    
@user2984982 Can you explain why accepted then unaccepted my answer? Perhaps I can help you more... Just because you've accepted a wrong answer to the given question. –  Paulo Freitas Nov 16 '13 at 3:14

2 Answers 2

up vote -1 down vote accepted

Sounds like you need DOMDocument. Specifically, the getElementsByTagName function. So using your example, I suggest this. Please adjust to suit your needs:

// Get the contents of the URL. 
$content = file_get_contents('url');

// Parse the HTML using `DOMDocument`
$dom = new DOMDocument();
@$dom->loadHTML($content);

// Search the parsed DOM structure for `span` elements.
$option = array();    
foreach($dom->getElementsByTagName('span') as $span){
  $option[] = $span->nodeValue;
}

// Dumps the values in `option` for review.    
echo '<pre>';
print_r($option);
echo '</pre>';
share|improve this answer
    
Why not use the loadHTMLFile() method instead? :) –  Amal Murali Nov 13 '13 at 19:41
    
Because I am coding an example using the original poster’s original example so they can see where they start off and the rest happens. ;) –  JakeGould Nov 13 '13 at 19:45
    
hi , I'm unable to get it to work . here is the url by the way : sayuri.co.jp/used-cars/… –  user2984982 Nov 13 '13 at 20:09
    
Result : pastebin.com/eknq8YfN –  user2984982 Nov 13 '13 at 20:11
    
It’s a coding example based on the limited info that was provided. Now—as I have said—please adjust to suit your needs. –  JakeGould Nov 13 '13 at 21:13

With the source code everything is easier:

<?php

$dom = new DOMDocument;
@$dom->loadHTMLFile('http://www.sayuri.co.jp/used-cars/B37659-Nissan-Tiida%20Latio-japanese-used-cars');
$xpath = new DOMXPath($dom);
$nodes = iterator_to_array($xpath->query('//h4/following-sibling::div')->item(0)->childNodes);
$items = array_map(function ($node) {
        return $node->nodeValue;
    }, array_filter($nodes, function ($node) {
        return $node->nodeValue != '/';
    }));
var_dump($items);

This gave me the following:

array(9) {
  [0]=>
  string(9) "A/C:front"
  [2]=>
  string(16) "Anti-Lock Brakes"
  [4]=>
  string(16) "Passenger Airbag"
  [6]=>
  string(13) "Power Mirrors"
  [8]=>
  string(14) "Power Steering"
  [10]=>
  string(13) "Power Windows"
  [12]=>
  string(13) "Driver Airbag"
  [14]=>
  string(12) "No Accidents"
  [16]=>
  string(16) "Power Door Locks"
}

You might want to use array_values() on $items to reset the indexes. That's all!

share|improve this answer

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.