july77
july77

Reputation: 823

Why array result is duplicated?

Can anyone help test this code and tell me what the error is? The expected result is 01223658060102111111. but it is duplicated like this

01223658060102111111012236580601021111110122365806010211111101223658060102111111

here is my code

<?php
ini_set('user_agent', 'My-Application/2.5'); //without this file_get_content would not work
$saveURL = fopen("url.txt", "w");
$html = file_get_contents("http://www.carlist.my/used-cars/2592832/2004-toyota-camry-2-0.html");
    $dom = new DOMDocument();
    @$dom->loadHTML($html);
    $xpath = new DOMXPath($dom);


   $cont = $xpath->evaluate("//ul[contains(@class, 'list-contact')]/li");
   foreach($cont as $con){
    echo $con->nodeValue;
   }

Upvotes: 0

Views: 63

Answers (2)

Haktan Suren
Haktan Suren

Reputation: 651

@july77: How can I split them to get result 0122365806 and 060102111111

   $arr = array();
   foreach($cont as $con){
    $arr[] = $con->nodeValue;
   }
   $first = $arr[0];
   $second = $arr[1];

Upvotes: 1

Haktan Suren
Haktan Suren

Reputation: 651

Replace this line to

$cont = $xpath->evaluate("//ul[contains(@class, 'list-contact')]/li");

to

$cont = $xpath->evaluate("//ul[contains(@class, 'list-contact')][1]/li");

Upvotes: 1

Related Questions