-2

From the following structure:

image shows the div from where to get data

I'm trying to fetch the marked text with the following code:

$price_new='div/div[@class="cat_price"]/text()';

if ($price_new!=null && $node = $Website_Xpath->query ($price_new, $row )) {
                    $result [$value] ['Price'] = $node->item( 0 )->nodeValue;


                } else {
                    $result [$value] ['Price'] = "";
                }

but the node value is NULL. How do I fetch the number correctly?

Ruslan Osmanov
  • 20,486
  • 7
  • 46
  • 60
  • I actually would not believe that someone could put a screenshot of code to Stackoverflow if I was not looking at it right now. :-D – Pontiac_CZ Dec 12 '18 at 09:23

2 Answers2

0

Your $Website_Xpath looks like an object of DOMXPath. Then the main issue with your code is in the XPath expression: 'div/div[@class="cat_price"]/text()'. You are trying to fetch a div from nowhere. Whether provide full path from the root node (e.g. /html/body/div), or select all divs with // prefix.

Example

$xml = <<<'XML'
<body>
  <div class="cat_price">
    <div class="was">67,000 - PKR</div>

          64,9999<span> - PKR</span>
  </div>
</body>
XML;

$doc = new DOMDocument();
$doc->loadXML($xml);

$text = '';
$xpath = new DOMXPath($doc);
// Select all text nodes within a <div> having class="cat_price"
if ($nodes = $xpath->query('//div[@class="cat_price"]/text()')) {
  // Search for a node with some content, except spaces
  foreach ($nodes as $n) {
    if ($text = trim($n->nodeValue))
      break;
  }
}
var_dump($text);

Output

string(7) "64,9999"
Ruslan Osmanov
  • 20,486
  • 7
  • 46
  • 60
  • I have put the same code written above, When i try to get total div value it gave me the whole but when trying to get nodes text value its passing me null – Junaid_Quadri Nov 15 '16 at 11:06
  • @Junaid_Quadri, the code in this answer [actually works](https://eval.in/677911). You must be doing something wrong on your side. – Ruslan Osmanov Nov 15 '16 at 11:23
0

You should provide the actual snippet, not just a screenshot of it. If I interpreted the screenshot correctly the snippet is something like:

$xml = <<<'XML'
<body>
  <div class="cat_price">
    <div class="was">67,000 - PKR</div>
    "
          64,9999"<span> - PKR</span>
  </div>
</body>
XML;

The text node with the price is the following sibling of the div with the class was. So it is possible to fetch it using that axis:

$document = new DOMDocument();
$document->loadXml($xml);
$xpath = new DOMXpath($document);

$expression = 'string(//div[@class="cat_price"]
   /div[@class="was"]/following-sibling::text()[1])';

var_dump($xpath->evaluate($expression));

Unlike DOMXpath::query(), DOMXpath::evaluate() can return scalar values depending on the expression. A string cast or a string function will return a string.

string(25) "
    "
          64,9999""

However the result will not only contain the number but the quotes and some whitespaces. translate() and normalize-space() could be used to clean it up:

$expression = 'normalize-space(
  translate(//div[@class="cat_price"]
    /div[@class="was"]/following-sibling::text()[1], \'"\', " ")
)';

var_dump($xpath->evaluate($expression));

Output:

string(7) "64,9999"
ThW
  • 19,120
  • 3
  • 22
  • 44