Jump to content

Help using cURL and preg_match with an array


tjc19999

Recommended Posts

I have the following SNIPPET of code:

while($z==0){
if($cityarrayalt[$z]!=""){
$curl_handle2=curl_init();
	curl_setopt($curl_handle2,CURLOPT_URL,"http://www.searchbug.com/tools/zip-code-lookup.aspx?TYPE=city2zip&CITY=".preg_replace("/ /","+",$city[0][$z])."&STATE=".$state2);
	curl_setopt($curl_handle2, CURLOPT_RETURNTRANSFER, true);
	$sec = curl_exec($curl_handle2);
	curl_close($curl_handle2);
	preg_match("/<b>([0-9]{5})<\/b>/",$sec,$seczip);
	echo $seczip[1];

 

$city[0][$z] is an array with values of cities in a given state

 

I think the problem is that array $city is not being read as a string and cURL doesn't know how to interpret the value given. I tried entering a test string and it works, but there is something wrong with my array, see var_dump below:

array(2) { [0]=> array(47) { [0]=> string(21) "CAROL STREAM" [1]=> string(21) "CAROL STREAM" [2]=> string(21) "CAROL STREAM" [3]=> string(21) "CAROL STREAM" [4]=> string(21) "CAROL STREAM" [5]=> string(21) "CAROL STREAM" [6]=> string(21) "CAROL STREAM" [7]=> string(25) "GLENDALE HEIGHTS" [8]=> string(21) "BLOOMINGDALE" [9]=> string(21) "BLOOMINGDALE" [10]=> string(16) "WHEATON" [11]=> string(17) "WINFIELD" [12]=> string(19) "GLEN ELLYN" [13]=> string(16) "ROSELLE" [14]=> string(19) "GLEN ELLYN" [15]=> string(16) "MEDINAH" [16]=> string(16) "WHEATON" [17]=> string(16) "ADDISON" [18]=> string(21) "HANOVER PARK" [19]=> string(21) "WEST CHICAGO" [20]=> string(16) "LOMBARD" [21]=> string(15) "ITASCA" [22]=> string(21) "WEST CHICAGO" [23]=> string(17) "BARTLETT" [24]=> string(19) "SCHAUMBURG" [25]=> string(19) "VILLA PARK" [26]=> string(18) "WOOD DALE" [27]=> string(19) "SCHAUMBURG" [28]=> string(19) "SCHAUMBURG" [29]=> string(20) "WARRENVILLE" [30]=> string(14) "WAYNE" [31]=> string(19) "STREAMWOOD" [32]=> string(19) "SCHAUMBURG" [33]=> string(17) "ELMHURST" [34]=> string(26) "ELK GROVE VILLAGE" [35]=> string(20) "BENSENVILLE" [36]=> string(14) "LISLE" [37]=> string(20) "BENSENVILLE" [38]=> string(22) "DOWNERS GROVE" [39]=> string(18) "WOOD DALE" [40]=> string(24) "HOFFMAN ESTATES" [41]=> string(18) "OAK BROOK" [42]=> string(19) "NAPERVILLE" [43]=> string(26) "ELK GROVE VILLAGE" [44]=> string(17) "HINSDALE" [45]=> string(19) "SCHAUMBURG" [46]=> string(19) "SCHAUMBURG" } [1]=> array(47) { [0]=> string(12) "CAROL STREAM" [1]=> string(12) "CAROL STREAM" [2]=> string(12) "CAROL STREAM" [3]=> string(12) "CAROL STREAM" [4]=> string(12) "CAROL STREAM" [5]=> string(12) "CAROL STREAM" [6]=> string(12) "CAROL STREAM" [7]=> string(16) "GLENDALE HEIGHTS" [8]=> string(12) "BLOOMINGDALE" [9]=> string(12) "BLOOMINGDALE" [10]=> string(7) "WHEATON" [11]=> string( "WINFIELD" [12]=> string(10) "GLEN ELLYN" [13]=> string(7) "ROSELLE" [14]=> string(10) "GLEN ELLYN" [15]=> string(7) "MEDINAH" [16]=> string(7) "WHEATON" [17]=> string(7) "ADDISON" [18]=> string(12) "HANOVER PARK" [19]=> string(12) "WEST CHICAGO" [20]=> string(7) "LOMBARD" [21]=> string(6) "ITASCA" [22]=> string(12) "WEST CHICAGO" [23]=> string( "BARTLETT" [24]=> string(10) "SCHAUMBURG" [25]=> string(10) "VILLA PARK" [26]=> string(9) "WOOD DALE" [27]=> string(10) "SCHAUMBURG" [28]=> string(10) "SCHAUMBURG" [29]=> string(11) "WARRENVILLE" [30]=> string(5) "WAYNE" [31]=> string(10) "STREAMWOOD" [32]=> string(10) "SCHAUMBURG" [33]=> string( "ELMHURST" [34]=> string(17) "ELK GROVE VILLAGE" [35]=> string(11) "BENSENVILLE" [36]=> string(5) "LISLE" [37]=> string(11) "BENSENVILLE" [38]=> string(13) "DOWNERS GROVE" [39]=> string(9) "WOOD DALE" [40]=> string(15) "HOFFMAN ESTATES" [41]=> string(9) "OAK BROOK" [42]=> string(10) "NAPERVILLE" [43]=> string(17) "ELK GROVE VILLAGE" [44]=> string( "HINSDALE" [45]=> string(10) "SCHAUMBURG" [46]=> string(10) "SCHAUMBURG" } }

Can anyone help out with this?

The problem is this:

urltest=array(3) { [0]=> string(21) "CAROL STREAM" [1]=> string(21) "CAROL STREAM" [2]=> string(21) "CAROL STREAM" }
urltest2=array(3) { [0]=> string(12) "CAROL STREAM" [1]=> string(12) "CAROL STREAM" [2]=> string(12) "CAROL STREAM" }

urltest array is the array that was genereated from a previous cURL and preg_match_all function that does not work

urltest2 is a manually created array that works

Can someone explain the difference between these seemingly identical arrays?

In your var_dump, see that CAROL STREAM has 21 characters instead of 12? There seems to be some invisible characters, that's why.

 

Can you do this for me? $seczip[1][0][0] should be any element in the array, just to test. Tell me what the results are.

 

print_r(unpack('C*', $seczip[1][0][0]));

This is my final resolved code:

while($z==0){
if($cityarrayalt[$z]!=""){
$curl_handle2=curl_init();
	curl_setopt($curl_handle2,CURLOPT_URL,"http://www.searchbug.com/tools/zip-code-lookup.aspx?TYPE=city2zip&CITY=".preg_replace("/ /","+",$city[1][$z])."&STATE=".$state2);
	curl_setopt($curl_handle2, CURLOPT_RETURNTRANSFER, true);
	$sec = curl_exec($curl_handle2);
	curl_close($curl_handle2);
	preg_match("/<b>([0-9]{5})<\/b>/",$sec,$seczip);
	echo $seczip[1];

The issue is that the variable was $city[0][$z] but needed to be $city[1][$z] to grab only the text I wanted which was in the parentheses like my second preg_match...the hidden text was HTML tags which is why I could not SEE them.

 

This problem has been resolved!

Archived

This topic is now archived and is closed to further replies.

×
×
  • 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.