richardw Posted February 28, 2007 Share Posted February 28, 2007 My goal is to split one address field that has Street Number and Street into two fields. case type 1 address: 102 Bowen St might return "102 B" as the Street number and the Street name as "owen St" case type 2 address: 102-112 Reservoir ave might return "102" as the Street number and the Street name as "-112 Reservoir ave" or 1022-1024 Hope St might return "1022-10" as the Street number and the Street name as "24 Hope St" Most of the records process fine, I am perplexed as to why a few records to not seperate correctly. <?php $sql = "SELECT * FROM sidewalks WHERE street_nmbr = '' "; // so as to only select the out of format records $result = mysql_query($sql); while ($row = mysql_fetch_assoc($result)) { $id = $row["id"]; $str_len = $row["street"]; $i = 0; for ($i = 0; $i < $str_len; $i++) { $current_char = substr($str,$i,1); if ($current_char == ' ') { $cutlength = $i; break; } else { } $st_nmbr = (substr($row["street"], 0, $cutlength)); $st_name = (substr($row["street"], $cutlength, $str_len)); // when it's working I will update the records // mysql_query("UPDATE sidewalks SET nmbr1 = '$st_nmbr', streetn2 = '$st_name' WHERE id =$id"); echo " Street Number: ", $st_nmbr; echo " - Street ", $st_name; ?> Any help will be greatly appreciated. Link to comment https://forums.phpfreaks.com/topic/40576-solved-trouble-splitting-an-address-into-two-fields-works-on-most-records/ Share on other sites More sharing options...
Caesar Posted February 28, 2007 Share Posted February 28, 2007 <?php $string = '1078-001 Bowen Street'; preg_match('/([\-0-9]+)(.*)/', $string, $address); $address[number] = $address[1]; $address[street] = $address[2]; ?> Link to comment https://forums.phpfreaks.com/topic/40576-solved-trouble-splitting-an-address-into-two-fields-works-on-most-records/#findComment-196348 Share on other sites More sharing options...
richardw Posted February 28, 2007 Author Share Posted February 28, 2007 Thanks Caesar!! Richard Link to comment https://forums.phpfreaks.com/topic/40576-solved-trouble-splitting-an-address-into-two-fields-works-on-most-records/#findComment-196358 Share on other sites More sharing options...
Caesar Posted February 28, 2007 Share Posted February 28, 2007 No problem...happy coding. Link to comment https://forums.phpfreaks.com/topic/40576-solved-trouble-splitting-an-address-into-two-fields-works-on-most-records/#findComment-196362 Share on other sites More sharing options...
boo_lolly Posted February 28, 2007 Share Posted February 28, 2007 it can also be done by using split() <?php $string = "1234 Some Street"; $address_array = split(" ", $string); print_r($address_array); ?> Link to comment https://forums.phpfreaks.com/topic/40576-solved-trouble-splitting-an-address-into-two-fields-works-on-most-records/#findComment-196366 Share on other sites More sharing options...
Recommended Posts
Archived
This topic is now archived and is closed to further replies.