Nuv Posted March 1, 2011 Share Posted March 1, 2011 I am writing a data scraping script but i am getting fatal error.Can someone please help me. My code:- <?php $con = mysql_connect("localhost","root",""); mysql_select_db("cakeshop", $con); $test = '<form name="select"> <p><font color="#000000" size="3"><select name="city" size="1" onchange="self.location.href=this.options[this.selectedIndex].value"> <option value="xxxwi.htm">Wisconsin</option> <option value="xxxwy.htm">Wyoming</option> </select><br clear="all"> </font><font color="#000000" size="2">Click on the state below where you want to send cake<br> or use the pull down menu above </font> </form> '; preg_match_all('~<option\s+value="(.*?)">(.*?)</option>~', $test, $out); for($i=0;$i<=2;$i++) { $data = file_get_contents('http://xxx.com/'.$out[1][$i].''); preg_match_all('~<option\s+value="(.*?)">(.*?)</option>~', $data, $city); //$klimit = sizeof($city); $klimit = count($city[1]); for($k=2 ;$k < $klimit; $k++) { $data = file_get_contents('http://xxx.com/'.$city[1][$k].''); //Error on this line $regex = '~<td\s+colspan="2"\s+width="350"><font\s+size="2">\s+<b>\s+(.*?) <\/b><br>(.*?) <br>(.*?),\s+(.*?)\s+<br>(.*?), (.*?)\s+<BR><BR><font\s+size="2"><img\s+src="\.\.\/images\/phone1.gif"\s+align="left"\s+hspace="4"\s+alt\s+=(.*)>\s+-\s+Phone\s+#\s+(.*?)\s+<\/font>\s+<BR>\s+<font\s+size\s+="1">~'; preg_match_all($regex, $data, $final); $jlimit = count($final[1]); for($j=0 ;$j < $jlimit; $j++) { $name = $final[1][$j]; $name = mysql_real_escape_string($name); $address = $final[2][$j]; $address = mysql_real_escape_string($address); $city = $final[3][$j]; $city = mysql_real_escape_string($city); $state = $final[4][$j]; $state = mysql_real_escape_string($state); $pincode = $final[6][$j]; $pincode = mysql_real_escape_string($pincode); $telephone = $final[8][$j]; $telephone = mysql_real_escape_string($telephone); mysql_query("INSERT INTO cakeshop (name, address, city, state, pincode, telephone) VALUES ('$name', '$address', '$city', '$state', '$pincode', '$telephone')"); } } } mysql_close($con); echo "Data scraping completed"; ?> Error message :- Fatal error: Cannot use string offset as an array in C:\Users\Booone\AppData\Roaming\NuSphere\PhpED\projects\scraping1.php Error is on this line "$data = file_get_contents('http://xxx.com/'.$city[1][$k].'');" Quote Link to comment https://forums.phpfreaks.com/topic/229260-string-offset-as-an-array-error/ Share on other sites More sharing options...
kenrbnsn Posted March 1, 2011 Share Posted March 1, 2011 What is printed if you do <?php echo '<pre> ' . print_r($city,true) . '</pre>'; ?> Ken Quote Link to comment https://forums.phpfreaks.com/topic/229260-string-offset-as-an-array-error/#findComment-1181334 Share on other sites More sharing options...
Nuv Posted March 1, 2011 Author Share Posted March 1, 2011 Where should i add above mentioned code ?After which line ? Quote Link to comment https://forums.phpfreaks.com/topic/229260-string-offset-as-an-array-error/#findComment-1181373 Share on other sites More sharing options...
kenrbnsn Posted March 1, 2011 Share Posted March 1, 2011 After the line that sets the value of $city, probably <?php preg_match_all('~<option\s+value="(.*?)">(.*?)</option>~', $data, $city); ?> Ken Quote Link to comment https://forums.phpfreaks.com/topic/229260-string-offset-as-an-array-error/#findComment-1181378 Share on other sites More sharing options...
Nuv Posted March 2, 2011 Author Share Posted March 2, 2011 Array ( [0] => Array ( [0] => Cities in Alabama [1] => [2] => Abbeville [3] => Abernant [4] => Adamsville [5] => Addison [6] => Adger [7] => Akron [8] => Alabaster [9] => Albertville ) [1] => Array ( [0] => cakes.pl?=AL,error [1] => cakes.pl?=AL,error [2] => cakes.pl?=AL,Abbeville [3] => cakes.pl?=AL,Abernant [4] => cakes.pl?=AL,Adamsville [5] => cakes.pl?=AL,Addison [6] => cakes.pl?=AL,Adger [7] => cakes.pl?=AL,Akron [8] => cakes.pl?=AL,Alabaster [9] => cakes.pl?=AL,Albertville ) [2] => Array ( [0] => Cities in Alabama [1] => [2] => Abbeville [3] => Abernant [4] => Adamsville [5] => Addison [6] => Adger [7] => Akron [8] => Alabaster [9] => Albertville ) ) Fatal error: Cannot use string offset as an array in C:\Users\bOONE\AppData\Roaming\NuSphere\PhpED\projects\scraping1.php Quote Link to comment https://forums.phpfreaks.com/topic/229260-string-offset-as-an-array-error/#findComment-1181660 Share on other sites More sharing options...
gizmola Posted March 2, 2011 Share Posted March 2, 2011 At some point during runtime the arrays you create using preg_match_all might become invalid because there was no match. You can try to add some error checking above, so for example: if (is_array($city)) { // go head }; See if adding that check prevents the runtime error. Quote Link to comment https://forums.phpfreaks.com/topic/229260-string-offset-as-an-array-error/#findComment-1181745 Share on other sites More sharing options...
Nuv Posted March 2, 2011 Author Share Posted March 2, 2011 Ah thankyou. That did it.However my script is skipping loops. Only taking first city in each State. Anything wrong you see in my script that would make it skip loops. However that error problem has been solved. Quote Link to comment https://forums.phpfreaks.com/topic/229260-string-offset-as-an-array-error/#findComment-1181749 Share on other sites More sharing options...
gizmola Posted March 2, 2011 Share Posted March 2, 2011 I can only suggest that you use the method kenrbsn suggested to make sure that the arrays you're iterating through are what you expect them to be. You might want to post the latest version of your code as well. Quote Link to comment https://forums.phpfreaks.com/topic/229260-string-offset-as-an-array-error/#findComment-1181751 Share on other sites More sharing options...
Nuv Posted March 2, 2011 Author Share Posted March 2, 2011 My data scraping script is as follows.However its not pulling out info for each and every city in the state but just 1. 1 City every State. Can someone please point me what i'm doing wrong My complete code :- <?php $con = mysql_connect("localhost","root",""); mysql_select_db("flourists", $con); $test = '<form name="select"> <p><font color="#000000" size="3"><select name="city" size="1" onchange="self.location.href=this.options[this.selectedIndex].value"> <option value="floristfinderal.htm">Alabama</option> <option value="floristfinderak.htm">Alaska</option> <option value="floristfinderaz.htm">Arizona</option> <option value="floristfinderar.htm">Arkansas</option> <option value="floristfinderca.htm">California</option> <option value="floristfinderco.htm">Colorado</option> <option value="floristfinderct.htm">Connecticut</option> <option value="floristfinderde.htm">Delaware</option> <option value="florists.pl?=DC,Washington">District of Columbia</option> <option value="floristfinderfl.htm">Florida</option> <option value="floristfinderga.htm">Georgia</option> <option value="floristfinderhi.htm">Hawaii</option> <option value="floristfinderid.htm">Idaho</option> <option value="floristfinderil.htm">Illinois</option> <option value="floristfinderin.htm">Indiana</option> <option value="floristfinderia.htm">Iowa</option> <option value="floristfinderks.htm">Kansas</option> <option value="floristfinderky.htm">Kentucky</option> <option value="floristfinderla.htm">Louisiana</option> <option value="floristfinderme.htm">Maine</option> <option value="floristfindermd.htm">Maryland</option> <option value="floristfinderma.htm">Massachusetts</option> <option value="floristfindermi.htm">Michigan</option> <option value="floristfindermn.htm">Minnesota</option> <option value="floristfinderms.htm">Mississippi</option> <option value="floristfindermo.htm">Missouri</option> <option value="floristfindermt.htm">Montana</option> <option value="floristfinderne.htm">Nebraska</option> <option value="floristfindernv.htm">Nevada</option> <option value="floristfindernh.htm">New Hampshire</option> <option value="floristfindernj.htm">New Jersey</option> <option value="floristfindernm.htm">New Mexico</option> <option value="floristfinderny.htm">New York</option> <option value="floristfindernc.htm">North Carolina</option> <option value="floristfindernd.htm">North Dakota</option> <option value="floristfinderoh.htm">Ohio</option> <option value="floristfinderok.htm">Oklahoma</option> <option value="floristfinderor.htm">Oregon</option> <option value="floristfinderpa.htm">Pennsylvania</option> <option value="floristfinderri.htm">Rhode Island</option> <option value="floristfindersc.htm">South Carolina</option> <option value="floristfindersd.htm">South Dakota</option> <option value="floristfindertn.htm">Tennessee</option> <option value="floristfindertx.htm">Texas</option> <option value="floristfinderut.htm">Utah</option> <option value="floristfindervt.htm">Vermont</option> <option value="floristfinderva.htm">Virginia</option> <option value="floristfinderwa.htm">Washington</option> <option value="floristfinderwv.htm">West Virginia</option> <option value="floristfinderwi.htm">Wisconsin</option> <option value="floristfinderwy.htm">Wyoming</option> </select><br clear="all"> </font><font color="#000000" size="2">Click on the state below where you want to send flowers<br> or use the pull down menu above </font> </form> '; preg_match_all('~<option\s+value="(.*?)">(.*?)</option>~', $test, $out); // Pulling out State name //echo '<pre> ' . print_r($out,true) . '</pre>'; for($i=0;$i<=50;$i++) { $data = file_get_contents('http://americas-florists.com/'.$out[1][$i].''); //Pulling out city name from each state preg_match_all('~<option\s+value="(.*?)">(.*?)</option>~', $data, $city); //echo '<pre> ' . print_r($city,true) . '</pre>'; $klimit = count($city[1]); for($k=0 ;$k < $klimit; $k++) { if (is_array($city)) { $anotherdata = file_get_contents('http://americas-florists.com/'.$city[1][$k].''); // Pulling out the information $regex = '~<td\s+colspan="2"\s+width="350"><font\s+size="2">\s+<b>\s+(.*?) <\/b><br>(.*?) <br>(.*?),\s+(.*?)\s+<br>(.*?), (.*?)\s+<BR><BR><font\s+size="2"><img\s+src="\.\.\/images\/phone1.gif"\s+align="left"\s+hspace="4"\s+alt\s+=(.*)>\s+-\s+Phone\s+#\s+(.*?)\s+<\/font>\s+<BR>\s+<font\s+size\s+="1">~'; preg_match_all($regex, $anotherdata, $final); $jlimit = count($final[1]); for($j=0 ;$j < $jlimit; $j++) { $name = $final[1][$j]; $name = mysql_real_escape_string($name); $address = $final[2][$j]; $address = mysql_real_escape_string($address); $city = $final[3][$j]; $city = mysql_real_escape_string($city); $state = $final[4][$j]; $state = mysql_real_escape_string($state); $pincode = $final[6][$j]; $pincode = mysql_real_escape_string($pincode); $telephone = $final[8][$j]; $telephone = mysql_real_escape_string($telephone); mysql_query("INSERT INTO flourists (name, address, city, state, pincode, telephone) VALUES ('$name', '$address', '$city', '$state', '$pincode', '$telephone')"); } } } } mysql_close($con); echo "Data scraping completed"; ?> Quote Link to comment https://forums.phpfreaks.com/topic/229260-string-offset-as-an-array-error/#findComment-1181766 Share on other sites More sharing options...
Nuv Posted March 2, 2011 Author Share Posted March 2, 2011 Funny thing when i add echo '<pre> ' . print_r($final,true) . '</pre>'; I see whole list of array that is supposed to be. But for some reason they aren't going into the database. Quote Link to comment https://forums.phpfreaks.com/topic/229260-string-offset-as-an-array-error/#findComment-1181784 Share on other sites More sharing options...
Nuv Posted March 2, 2011 Author Share Posted March 2, 2011 Problem solved.Wrote $city twice. One array other string. Script is working properly. Quote Link to comment https://forums.phpfreaks.com/topic/229260-string-offset-as-an-array-error/#findComment-1181827 Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.