21e6 Trouble pulling data using reg exp. - Writing PHP
Training, Open Source computer languages
PerlPHPPythonMySQLApache / TomcatTclRubyJavaC and C++LinuxCSS 
Search for:
Home Accessibility Courses Diary The Mouth Forum Resources Site Map About Us Contact
Trouble pulling data using reg exp.

Posted by Chris_Isaac (Chris Isaac), 16 June 2003
Hi

I'm trying to pull out the County out of out of an address field ($addy).  I can get either the town and county together or the county and postcode.  I tried to do another reg exp on the result of the 2nd option above, but it just prints out 'Array'.  Any ideas please??

Chris
Code:
<?php

$fh =  fopen("/usr/home/mysite/testdata.txt","r");
while (list($place,$accom,$phone,$addy,$email) = fgetcsv($fh,10000)) {

ereg('([[:alpha:]]{1,2}[[:digit:]]{1,2})',$addy,$gotten);# 1st half of postcode
ereg('([[:alpha:]]{1,2}[[:digit:]]{1,2}[[:space:]][[:digit:]]{1,2}[[:alpha:]]{1,2})',$addy,$postcode);# full postcode
ereg('([[:alpha:]]{1})',$place,$pletter);# grab 1st letter of place
preg_match("/$place\S\s\w+/i", $addy,$towncoun);
preg_match("/\w+,\s\w{1,2}\d{1,2}/i", $addy,$counte);
preg_match("/\w+/i", $counte,$county);

print "$place, $accom, $phone, $addy, $email, $gotten[0], $postcode[0], $towncoun[0], $counte[0], $county[0]<br><br>";
}
?>


Posted by admin (Graham Ellis), 16 June 2003
This code:

Code:
<?php

$fh =  fopen("./cache/results.txt","r");
while (list($place,$accom,$phone,$addy,$email) = fgetcsv($fh,10000)) {
       ereg(',[[:space:]]*([^[:space:]]+),[[:space:]]*([[:alpha:]]{1,2}[[:digit:]]{1,2})....$',$addy,$gotten);
       print "County is $gotten[1] area is $gotten[2]<br>";
}
?>


on similar (?) address data gave me ouput including ...

Quote:
County is Wiltshire area is SN12
County is Wiltshire area is SN12
County is Wiltshire area is SN12
County is Wiltshire area is SN12
County is Wiltshire area is SN12
County is Somerset area is BA1
County is Somerset area is BA2
County is Somerset area is BA2
County is Somerset area is BA1


My addresses had comma separated lines and I've assumed that the chunk before the postcode was the county.  In practice, the county was missing on some of the data and I got towns such as "Bath" reported back for a few counties.   HOWEVER - it's the program you're really looking at.

I've pub (extra) round brackets around the county, and I've referred to $gotten[1] for the first bracket match and $gotten[2] for the second bracket match.  $gotten[0] would refer to the whole match string (not of any interest to me) and $gotten just says "Array".  By the way - NO SPACE between $gotten and [1] within double quotes - if you do, it says Array [1] ....

Posted by Chris_Isaac (Chris Isaac), 16 June 2003
Thanks, thats cured it!



This page is a thread posted to the opentalk forum at www.opentalk.org.uk and archived here for reference. To jump to the archive index please follow this link.
933b

You can Add a comment or ranking to this page

© WELL HOUSE CONSULTANTS LTD., 2013: Well House Manor • 48 Spa Road • Melksham, Wiltshire • United Kingdom • SN12 7NY
PH: 01144 1225 708225 • FAX: 01144 1225 899360 • EMAIL: info@wellho.net • WEB: http://www.wellho.net • SKYPE: wellho
0