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 Smith’. 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.
@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.