homer.favenir Posted May 2, 2008 Share Posted May 2, 2008 hi to all, i have a simple, just a simple concern. how to extract address, city and phone in this txt file? RL ACCOUNTANTS WAREHOUSE 6070 MC*DONOUGH DR N*W NORC 770 326-6302V2 RL ACCOUN*TAX PROFESSIONAL 770 447-1244V2 RL ACCOUNTAX SERVICE 6000 LIVE OAK PKWY N*W NORC 678 206-0361V2 can i use preg_match()? any advice? thanks ??? Quote Link to comment https://forums.phpfreaks.com/topic/103852-extract-address-city-phone/ Share on other sites More sharing options...
sasa Posted May 2, 2008 Share Posted May 2, 2008 try <?php $a = 'RL ACCOUNTANTS WAREHOUSE 6070 MC*DONOUGH DR N*W NORC 770 326-6302V2 RL ACCOUN*TAX PROFESSIONAL 770 447-1244V2 RL ACCOUNTAX SERVICE 6000 LIVE OAK PKWY N*W NORC 678 206-0361V2'; $a = explode("\n",$a); $final = array(); foreach ($a as $row => $line){ $data[0] = trim(substr($line, 0, 16)); $data[1] = trim(substr($line, 16,125)); $data[2] = trim(substr($line,141, 55)); $data[3] = trim(substr($line,196, 32)); $data[4] = trim(substr($line,228, 12)); $data[5] = trim(substr($line,240, 2)); $final[] = $data; } print_r($final); ?> Quote Link to comment https://forums.phpfreaks.com/topic/103852-extract-address-city-phone/#findComment-531637 Share on other sites More sharing options...
homer.favenir Posted May 3, 2008 Author Share Posted May 3, 2008 thanks!, i comeup with this script from your script $a = file('gin002.txt'); /*$a = 'RL ACCOUNTANTS WAREHOUSE 6070 MC*DONOUGH DR N*W NORC 770 326-6302V2 RL ACCOUN*TAX PROFESSIONAL 770 447-1244V2 RL ACCOUNTAX SERVICE 6000 LIVE OAK PKWY N*W NORC 678 206-0361V2';*/ foreach ($a as $row => $line){ //$data[0] = trim(substr($line, 0, 16))."<br />"; //$data[1] = trim(substr($line, 16,125))."<br />"; $data[2] = trim(substr($line,141, 55))."<br />"; $data[3] = trim(substr($line,196, 32))."<br />"; $data[4] = trim(substr($line,228, 14))."<br />"; //$data[5] = trim(substr($line,240, 2))."<br />"; $final[] = $data; } //$a = explode("\n",$a); print_r($final)."<br />"; and the result is this. Array ( [0] => Array ( [2] => [3] => [4] => ) [1] => Array ( [2] => [3] => SNVL [4] => 770 979-2966 ) [2] => Array ( [2] => 6070 MC*DONOUGH DR N*W [3] => NORC [4] => 770 326-6302 ) [3] => Array ( [2] => [3] => [4] => 770 447-1244 ) [4] => Array ( [2] => 6000 LIVE OAK PKWY N*W [3] => NORC [4] => 678 206-0361 ) [5] => Array ( [2] => 4995-D FRIENDSHIP RD [3] => BUFORD [4] => 770 614-6440 ) [6] => Array ( [2] => 2624 GLADSTONE TERRACE [3] => WDSTK [4] => 770 846-7799 ) [7] => Array ( [2] => 4455 COMMERCE DR N*E [3] => BUF [4] => 770 271-0051 ) [8] => Array ( [2] => 2265 SUGARBIRCH DR S*W and so on....... how can i remove Array ( [0] => Array ( [2] => in the result? Quote Link to comment https://forums.phpfreaks.com/topic/103852-extract-address-city-phone/#findComment-532249 Share on other sites More sharing options...
sasa Posted May 3, 2008 Share Posted May 3, 2008 <?php $a = file('gin002.txt'); /* $a = 'RL RL ACCOUNTANTS WAREHOUSE 6070 MC*DONOUGH DR N*W NORC 770 326-6302V2 RL ACCOUN*TAX PROFESSIONAL 770 447-1244V2 RL ACCOUNTAX SERVICE 6000 LIVE OAK PKWY N*W NORC 678 206-0361V2'; $a = explode("\n",$a); */ foreach ($a as $row => $line){ //$data[0] = trim(substr($line, 0, 16))."<br />"; //$data[1] = trim(substr($line, 16,125))."<br />"; $data[2] = trim(substr($line,141, 55))."<br />"; $data[3] = trim(substr($line,196, 32))."<br />"; $data[4] = trim(substr($line,228, 14))."<br />"; //$data[5] = trim(substr($line,240, 2))."<br />"; if ($data[2]!= "<br />" or $data[3] != "<br />" or $data[4] != "<br />") $final[] = $data; } //$a = explode("\n",$a); print_r($final)."<br />"; ?> Quote Link to comment https://forums.phpfreaks.com/topic/103852-extract-address-city-phone/#findComment-532254 Share on other sites More sharing options...
homer.favenir Posted May 3, 2008 Author Share Posted May 3, 2008 great! it extracted only the address city and phone! and it only use foreach, trim and substr functions. can the Array ( [0] => Array (" not be displayed?can it be removed? undisplay the "array" word... i comeup with this script $a = file('gin002.txt'); /* $a = 'RL RL ACCOUNTANTS WAREHOUSE 6070 MC*DONOUGH DR N*W NORC 770 326-6302V2 RL ACCOUN*TAX PROFESSIONAL 770 447-1244V2 RL ACCOUNTAX SERVICE 6000 LIVE OAK PKWY N*W NORC 678 206-0361V2'; $a = explode("\n",$a); */ foreach ($a as $row => $line){ //$data[0] = trim(substr($line, 0, 16))."<br />"; //$data[1] = trim(substr($line, 16,125))."<br />"; $data[2] = trim(substr($line,141, 55))."<br />"; $data[3] = trim(substr($line,196, 32))."<br />"; $data[4] = trim(substr($line,228, 14))."<br />"; //$data[5] = trim(substr($line,240, 2))."<br />"; if ($data[2]!= "<br />" or $data[3] != "<br />" or $data[4] != "<br />") $final[] = $data; } //$a = explode("\n",$a); for($i = 1;$i <=10; $i++) { $final[$i]; echo $data[2]; echo $data[3]; echo $data[4]; } //print_r($final)."<br />"; ?> but the result is only 1 record and it keeps on repeating. 3000 LANGFORD RD N*W NORCROSS 770 447-5283 3000 LANGFORD RD N*W NORCROSS 770 447-5283 3000 LANGFORD RD N*W NORCROSS 770 447-5283 3000 LANGFORD RD N*W NORCROSS 770 447-5283 3000 LANGFORD RD N*W NORCROSS 770 447-5283 3000 LANGFORD RD N*W NORCROSS 770 447-5283 3000 LANGFORD RD N*W NORCROSS 770 447-5283 3000 LANGFORD RD N*W NORCROSS 770 447-5283 3000 LANGFORD RD N*W NORCROSS 770 447-5283 3000 LANGFORD RD N*W NORCROSS 770 447-5283 By the way the $i is the number of words. thanks Quote Link to comment https://forums.phpfreaks.com/topic/103852-extract-address-city-phone/#findComment-532278 Share on other sites More sharing options...
sasa Posted May 3, 2008 Share Posted May 3, 2008 for($i = 0;$i <=10; $i++) { $data = $final[$i]; echo $data[2]; echo $data[3]; echo $data[4]; } Quote Link to comment https://forums.phpfreaks.com/topic/103852-extract-address-city-phone/#findComment-532282 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.