-3

How can I get the value of this text. Idea: Year: 2012 KM: 69.000 Color: Blue Price: 29.9000

preg_match('@</div></td><td
 class=\"searchResultsAttributeValue\">(.*?)<\/td>@si',$string,$val);

 $string = "<div class="classifiedSubtitle">Opel > Astra > 1.4 T Sport</div>
</td>
            <td class="searchResultsAttributeValue">
                    2012</td>
            <td class="searchResultsAttributeValue">
                    69.000</td>
            <td class="searchResultsAttributeValue">
                    Blue</td>
            <td class="searchResultsPriceValue">
                        <div> $ 29.900 </div></td>
                <td class="searchResultsDateValue">
                        <span>21 Nov</span>
                        <br/>
                        <span>2016</span>
                    </td>
                <td class="searchResultsLocationValue">
                        USA<br/>Texas</td>"
Erkan
  • 33
  • 2
  • 9
  • 1
    [Don't use a RegEx to parse HTML](http://stackoverflow.com/a/1732454/746383), use [DomDocument](http://php.net/manual/en/class.domdocument.php) instead – prehfeldt Oct 21 '16 at 14:04
  • First you need to encapsulate `$string` with single quotes, or escape the double quotes your attributes use. Here would be a closer regex, this will still fail though so you should use a parser, `\s*\s*(.*?)`. You also should format the `Idea:` part of your question. – chris85 Oct 21 '16 at 15:00
  • Possible duplicate of [How do you parse and process HTML/XML in PHP?](http://stackoverflow.com/questions/3577641/how-do-you-parse-and-process-html-xml-in-php) – chris85 Oct 21 '16 at 15:00

1 Answers1

1

The best solution isn't with regex. You should do it with Dom.

$dom = new DOMDocument();
$dom->loadHTML($string);
$xPath = new DOMXpath($dom);
$tdValue = $xPath->query('//td[@class="searchResultsAttributeValue"]')->get(0)->nodeValue;

This way you'll get the td element with the class searchResultsAttributeValue. Of course you should verify if this element really exists, and some other verifications but that's the way.

Hope I was helpful.

Vinicius Dias
  • 664
  • 3
  • 15