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 8432669
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 10, 20262026-06-10T06:11:27+00:00 2026-06-10T06:11:27+00:00

Here I am parsing a webpage and getting the ‘name’ field from that webpage.

  • 0

Here I am parsing a webpage and getting the ‘name’ field from that webpage. Here is the code that does the parsing:

foreach($dom->getElementsByTagName('table') as $table) {
    if($table->getAttribute('class')=='dataTable'){
        foreach($table->getElementsByTagName('tr') as $tr){
            if(isset($tr->getElementsByTagName('td')->item(0)->nodeValue)){
                $out[$i]['name'] = $tr->getElementsByTagName('td')->item(0)->nodeValue;
            }
        }
    }
}

In the webpage I am parsing I have the nodevalue for name in the form of ‘Mark&nbspSmith’. So when I get the result I have ‘name’ value as ‘Mark Smith’ in IDE and ‘Mark┬áSmith’ in command promt.

Now I want to split the ‘name’ string in such a way so that I get firstname(‘Mark’) and lastname(‘smith’) separately.

I tried:

explode(" ", $out[$i]['name']) as well as
explode(" " , $out[$i]['name'])

But nothing seems to work for me. Help me to split the string as firstname and lastname?

Hope I am clear with my question.

  • 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-10T06:11:28+00:00Added an answer on June 10, 2026 at 6:11 am

    @user1518659 here try this, to fix the issue just replace the   with a space before passing to DOMDocument, I also added the split of firstname last name 🙂 hope it helps.

    <?php 
    header('Content-Type: text/html; charset=utf-8'); //Required if your outputting, as the description contains utf-8 characters
    //Load the source (input)
    $html_source = file_get_contents('http://www.reuters.com/finance/stocks/companyOfficers?symbol=AOS');
    $html_source = str_replace('&nbsp;',' ',$html_source);
    
    //Dom document
    $dom = new DOMDocument('1.0');
    @$dom->loadHTML($html_source);
    
    $out =array();
    $i=0;
    foreach($dom->getElementsByTagName('table') as $table) {
        if($table->getAttribute('class')=='dataTable'){
    
            foreach($table->getElementsByTagName('tr') as $tr){
                if(isset($tr->getElementsByTagName('td')->item(0)->nodeValue)){
    
                    $out[$i]['fullname'] = $tr->getElementsByTagName('td')->item(0)->nodeValue;
    
                    $name = explode(' ',$out[$i]['fullname']);
                    $out[$i]['first_name'] = $name[0];
                    $out[$i]['last_name'] = $name[1];
    
                    if(!isset($tr->getElementsByTagName('td')->item(2)->nodeValue)){
    
                        foreach ($out as $key=>$value){
                            if($value['fullname'] == $tr->getElementsByTagName('td')->item(0)->nodeValue &&
                            !is_numeric(substr($tr->getElementsByTagName('td')->item(1)->nodeValue,0,1)) && 
                            $tr->getElementsByTagName('td')->item(1)->nodeValue != "--" ){
                                $out[$key]['description']= $tr->getElementsByTagName('td')->item(1)->nodeValue;
                            }
                        }
    
                    }else{
                        if(!isset($tr->getElementsByTagName('td')->item(2)->nodeValue)){continue;}
                        if(isset($tr->getElementsByTagName('td')->item(3)->nodeValue)){
                            $out[$i]['age']= $tr->getElementsByTagName('td')->item(1)->nodeValue;
                            $out[$i]['since']= $tr->getElementsByTagName('td')->item(2)->nodeValue;
                            $out[$i]['position']= $tr->getElementsByTagName('td')->item(3)->nodeValue;
                        }
                    }
                    $i++;
                }
            }
        }
    }
    
    //Clean up
    $return = array();
    foreach ($out as $key=>$row){
        if(isset($row['fullname']) && isset($row['age']) && isset($row['since']) && isset($row['position']) && isset($row['description'])){
            $return[$key] = $out[$key];
        }
    }
    
    
    print_r($return);
    
    /*
    Array
    (
        [0] => Array
            (
                [fullname] => Paul Jones
                [first_name] => Paul
                [last_name] => Jones
                [age] => 63
                [since] => 2011
                [position] => Chairman of the Board, Chief Executive Officer
                [description] => Mr. Paul W. Jones serves as the Chairman of the Board, Chief Executive Officer of A. O. Smith Corp. He has been a director of company since 2004. He is a member of the Investment Policy Committee of the Board. He was elected chairman of the board, president and chief executive officer effective December 31, 2005. He was president and chief operating officer from 2004 to 2005. Prior to joining the company, he was chairman and chief executive officer of U.S. Can Company, Inc. from 1998 to 2002. He previously was president and chief executive officer of Greenfield Industries, Inc. from 1993 to 1998 and president from 1989 to 1992. Mr. Jones has been a director of Federal Signal Corporation since 1998, where he chairs the Nominating and Governance Committee and is a member of the Compensation and Benefits Committee and the Executive Committee, and Integrys Energy Group, Inc. since 2011, where he is a member of the Compensation and Financial Committees. He was also a director of Bucyrus International, Inc. from 2006 until its acquisition by Caterpillar, Inc. in 2011, and chaired the Compensation Committee.
            )
    
        [1] => Array
            (
                [fullname] => Ajita Rajendra
                [first_name] => Ajita
                [last_name] => Rajendra
                [age] => 60
                [since] => 2011
                [position] => President, Chief Operating Officer, Director
                [description] => Mr. Ajita G. Rajendra serves as the President, Chief Operating Officer and Director of A. O. Smith Corp. He was elected a director of company in December 2011, based on the recommendation of the Nominating and Governance Committee, following his election as President and Chief Operating Officer in September 2011. Mr. Rajendra joined the company as President of A. O. Smith Water Products Company in 2005, and was named Executive Vice President of the company in 2006. Prior to joining the company, Mr. Rajendra was Senior Vice President at Kennametal, Inc., a manufacturer of cutting tools, from 1998 to 2004. Mr. Rajendra also serves on the board of Donaldson Company, Inc., where he is a member of the Audit Committee and Human Resources Committee. Further, Mr. Rajendra was a director of Industrial Distribution Group, Inc. from 2007 until its acquisition by Eiger Holdco, LLC in 2008.
            )
            ...
            ...
    */
    ?>
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I currently have a csv file that I'm parsing with an example from here:
I'm trying to learn some XML Parsing here and I've been given some code
The JSON that I am parsing can be found here 'redacted'. I can correctly
Here is my code: (source: http://www.androidhive.info/2011/11/android-xml-parsing-tutorial/ ) Here is my XML file: removed On
PHP/MySQL newbie here. I managed to develop a web page that displays info from
i here that libxml parsing is fast and for that downloading image is a
Having a hard time parsing XML created from a web-form using POST. Here's the
I have a script that parses certain elements from a webpage and stores them
I am parsing a file for particular keyword matching by C program, here is
I already looked at a similar question here : null pointer exception-parsing json with

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.