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
 
For 2023 (and 2024 ...) - we are now fully retired from IT training.
We have made many, many friends over 25 years of teaching about Python, Tcl, Perl, PHP, Lua, Java, C and C++ - and MySQL, Linux and Solaris/SunOS too. Our training notes are now very much out of date, but due to upward compatability most of our examples remain operational and even relevant ad you are welcome to make us if them "as seen" and at your own risk.

Lisa and I (Graham) now live in what was our training centre in Melksham - happy to meet with former delegates here - but do check ahead before coming round. We are far from inactive - rather, enjoying the times that we are retired but still healthy enough in mind and body to be active!

I am also active in many other area and still look after a lot of web sites - you can find an index ((here))
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.

You can Add a comment or ranking to this page

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