Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 8735441
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 13, 20262026-06-13T10:03:33+00:00 2026-06-13T10:03:33+00:00

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

  • 0

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

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-13T10:03:34+00:00Added an answer on June 13, 2026 at 10:03 am

    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")
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Using Java,I have to fetch multiple sets of values from an XML file to
Greetings, Here is my problem. I need to get data from multiple rows and
Ha ii every body,i am using accessory view checkmark for selection multiple values from
I need to get the keys from values that are duplicates. I tried to
Possible Duplicate: Returning multiple values from a C++ function /************************************************/ /* Name: premserv */
Needing a bit help getting multiple values from a string using Regex. I am
I need to return multiple STRING values from my backgroundworker in each loop, so
I've one listView with multiple choice and checkbox. I get the values from listview
I want to return multiple values from a PHP function, but it did not
I need to issue multiple $.get requests, process their responses, and record the results

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.