1

I need get multiple values from span tags using php, for example :

   <div>
    <span class='name'>name value</span>
    <span class='city'>city name</span>
    <span class='street'>street name</span>
    <span class='phone'>+17240000000</span>
    </div>
  <div>
   <span class='name'>name value 2</span>
   <span class='city'>city name 2</span>
   <span class='street'>street name 2</span>
   <span class='phone'>+17240000000 2</span>
  </div> 

I have to get span values using span class, i try this code to get the value, and it's working put for just 1 value such as "name" :

<?
$html = "<div>
         <span class='name'>name value</span>
         <span class='city'>city name</span>
         <span class='street'>street name</span>
         <span class='phone'>+17240000000</span>
         </div>
         <div>
         <span class='name'>name value 2</span>
         <span class='city'>city name 2</span>
         <span class='street'>street name 2</span>
         <span class='phone'>+17240000000 2</span>
         </div>";
$dom = new DOMDocument();
@$dom->loadHTML($html);
$dom_xpath = new DOMXPath($dom);

$entries = $dom_xpath->evaluate("//span[@class='name']");
foreach ($entries as $entry) {
   echo  $entry->nodeValue."<br />";
}
?>

Is there anyone to help me to retouch the code so i can get all values

Thanks

MagePal Extensions
  • 17,646
  • 2
  • 47
  • 62

1 Answers1

2

Try changing

......
$dom = new DOMDocument();
@$dom->loadHTML($html);

foreach($dom->getElementsByTagName('span') as $span){
  echo $span->nodeValue . '<br>';
  //echo $span->getAttribute('class');
}

Base on the above format you may want to try this (assuming that the className will never change and always present)

......
$dom = new DOMDocument();
@$dom->loadHTML($html);

$result = array();
foreach ($dom->getElementsByTagName('div') as $div){
    $temp = array();
    foreach($div->getElementsByTagName('span') as $span){
        $temp[$span->getAttribute('class')] = $span->nodeValue; 
    }

    // db insert here
    // You may want to do some error check before inserting the data by making sure the array key exist 
    // need to escape data (SQL_injection)
    //mysql_query("INSERT INTO db (`name`,`city`,`street`,`phone`) VALUES('{$temp['name']}','{$temp['city']}','{$temp['street']}','{$temp['phone']}')");

    $result[] = $temp;

}

print_r($result);

You need to escape you values before inserting them into db (sql injection) and a more efficient want of doing bulk insert -- Read more at insert multiple rows via a php array into mysql and http://en.wikipedia.org/wiki/SQL_injection

OR

 $entries = $dom_xpath->evaluate("//span[@class='name']")

to

 $entries = $dom_xpath->evaluate("//span")
Community
  • 1
  • 1
MagePal Extensions
  • 17,646
  • 2
  • 47
  • 62
  • looks good, but can I put it in the variable. such $name = entry[1]; and $phone=$entry[2]; this is just example so i can insert them in the database – user1778423 Oct 27 '12 at 00:20
  • Take a look at my update code... I am not too sure what you are trying to accomplish because you are going to have a issue with have 2 'name', 'city' etc. What you may want to do is do a 2 dimension array and get the value div by div ... arr[0][name] ... arr[1][name] – MagePal Extensions Oct 27 '12 at 00:29
  • thank you for fast replay, i want to insert name,city,street,phone into database. so when i put it in foreach the data will be insert one by one. sorry for my English and thanks again. mysql_query("INSERT INTO db (name,city,street,phone) VALUES('$name','$city','$street','$phone')") – user1778423 Oct 27 '12 at 00:38
  • Take a look at the code above...it should work the way you want now – MagePal Extensions Oct 27 '12 at 01:07
  • You may want to do some error check before inserting the data by making sure the array key exist – MagePal Extensions Oct 27 '12 at 01:11
  • everything is OK with insert the data, but i have some problem with charset encoding. i want to insert the data with chrset=windows-1256,because the data unreadable right now – user1778423 Oct 27 '12 at 01:37
  • i found it, its : mysql_query("SET NAMES utf8"); – user1778423 Oct 27 '12 at 01:45
  • Take a look @ http://stackoverflow.com/questions/6516589/how-i-can-solve-my-php-web-page-file-language-encoding.. plus mysql_real_escape_string should help – MagePal Extensions Oct 27 '12 at 01:48