PHP Scrape; save as variables for MySQL Insert

I successfully clear the website to get the space separated data on the page:

$html = file_get_contents("http://www.somewebsite.com");
$scores_doc = new DOMDocument();

$scores_doc->loadHTML($html);
$scores_path = new DOMXPath($scores_doc);
$scores_row  = $scores_xpath->query('//td[@class="first"]');

foreach($scores_row as $row){
    echo $row->nodeValue . "<br/>";
}

      

Output example:

23 Crimmons, Bob (CA)
48 Silas, Greg (RI)
82 Huston, Roger (TX)
21 Lester, Terry (NC)

      

Instead of echoing the output, I need to split that value into four smaller pieces and into variables (array or otherwise). I know the MySQL side very well, I just don't use PHP every day. I tried (instead of "echo" and after defining it as an array):

$data[] = echo $row->nodeValue;

      

+3


source to share


1 answer


A wrapper around the syntax to use : If you just want to assign a whole string 23 Crimmons, Bob (CA)

as one string to an array. You must use the correct syntax.

$data[] = echo $row->nodeValue;

      

Should be:

$data[] = $row->nodeValue;

      


Three possible solutions to your problem.

Solution 1: Improve cleansing

The best way to copy these four values ​​separately is with a more specific query. You can try updating your xpath query at the line:

$scores_xpath->query('//td[@class="first"]');

      

The request you can use depends on the structure of the page you are clearing.

Solution 2: Splitting a string using PHP explode

You can use the PHP explode function to split the string, but note that this will cause some problems when spaces are used in the name.



echo $row->nodeValue . "<br/>";

      

Maybe something like:

// Assuming that $row->nodeValue will have the string `23 Crimmons, Bob (CA)` as it value 
$explodeRow = explode(' ', $row->nodeValue);

/*
* $explodeRow now contains four values. 
*
* $explodeRow[0] = "23";
* $explodeRow[1] = "Crimmons,";
* $explodeRow[2] = "Bob";
* $explodeRow[3] = "(CA)";
*/

      

You can remove characters (

and )

in $explodeRow[3]

with PHP str_replace , preg_replace or substr for example.

Solution 3: Splitting a string using regular expressions

Alternatively, you can select the first two numbers first. Then choose the last part in between ()

. Then divide the remaining values ​​by ,

. But it can also create problems when using multiple commas.

An example of this solution would look something like this:

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

      

+4


source







All Articles