Jump to content

String offset as an array error


Nuv

Recommended Posts

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].'');"

 

Link to comment
Share on other sites

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 



Link to comment
Share on other sites

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.

Link to comment
Share on other sites

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.

Link to comment
Share on other sites

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.

 

 

Link to comment
Share on other sites

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"; 
?>

Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.