A sidenote on the used syntax:
If you just want to assign the whole 23 Crimmons, Bob (CA)
string as one string to an array. You should use the right syntax.
$data[] = echo $row->nodeValue;
Should be:
$data[] = $row->nodeValue;
Three possible solutions to your problem.
Solution 1: Improve scraping
The best way to scrape those four values seperately would be to query more specifically. You can try to update your xpath query on line:
$scores_xpath->query('//td[@class="first"]');
The query you can use depends on the structure of the page you're scraping.
Solution 2: Splitting string using PHP explode
You could use PHP's explode function to separate the string, but note that will give some problems when there are spaces used in a name.
echo $row->nodeValue . "<br/>";
Can be something like:
// Assuming that $row->nodeValue will have the string `23 Crimmons, Bob (CA)` as it's value
$explodeRow = explode(' ', $row->nodeValue);
/*
* $explodeRow now contains four values.
*
* $explodeRow[0] = "23";
* $explodeRow[1] = "Crimmons,";
* $explodeRow[2] = "Bob";
* $explodeRow[3] = "(CA)";
*/
You can choose to remove the (
and )
characters in $explodeRow[3]
with the PHP str_replace, preg_replace or substr function for example.
Solution 3: Splitting string using regular expressions
Alternatively you can decide to fetch the first two numbers first. Then to fetch the last part between ()
. Then seperate the two remaining values by ,
. But this can also generates problems when multiple commas are used.
Example of this solution will be, something like:
preg_match("~^(\d+)~", $row->nodeValue, $number);
$number[1]; # will be 23
preg_match("#\((.*?)\)#", $row->nodeValue, $last);
$last[1]; # will be CA
$middleExp = explode("(", $row->nodeValue, 2);
$middle = substr((strlen($number[1])-1), strlen($row->nodeValue), $middleExp[0]);
$middleExp2 = explode(",", $middle);
$middleL = $middleExp2[0]; # will be Crimmons
$middleR = $middleExp2[1]; # will be Bob