Hi everybody. If we want to grabbing any data on a web site source, we can use preg_match or preg_match_all function on PHP. Preg_match function is grab a data that can find first data and as preg_match_all function grab all data that can find datas.
For example:
Our HTML page’s(for example http://websiteaddress.com/test.html) source code:
<table style="width:100%"> <caption>Monthly savings</caption> <tr> <th>Month</th> <th>Savings</th> </tr> <tr> <td>January</td> <td>$100</td> </tr> <tr> <td>February</td> <td>$50</td> </tr> </table>
Our grabber web page’s code(with preg_match function):
$site=file_get_contents('http://websiteaddress.com/test.html'); preg_match('@<td>(.*?)</td>@si',$site,$myString); var_dump($myString);
Our output:
array(2) { [0]=> string(16) "January" [1]=> string(7) "January" }
Our grabber web page’s code(with preg_match_all function):
$site=file_get_contents('http://websiteaddress.com/test.html'); preg_match_all('@<td>(.*?)</td>@si',$site,$myString); var_dump($myString);
Our output:
array(2) { [0]=> array(4) { [0]=> string(16) "January" [1]=> string(13) "$100" [2]=> string(17) "February" [3]=> string(12) "$50" } [1]=> array(4) { [0]=> string(7) "January" [1]=> string(4) "$100" [2]=> string(8) "February" [3]=> string(3) "$50" } }