Re: Simple RegEx question

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

 



M5 schreef:
> I'm learning regular expressions, and trying to figure out what's
> possible and what's not. 

pretty much anything as far as string parsing goes.

> Any ideas of how to create a preg_match
> expression to parse following three lines:

yes. given your intention to learn regexps why not try to come
up with one? if you get stuck show us what you have so far ...
you don't learn to eat with cutlery by being spoonfed.

if you want to parse the whole string in one go you'll want to use
preg_match_all() otherwise use preg_match and iterate over each line ..

foreach(explode("\n", $data) as $line) {
	$matches = array();
	if (preg_match($line, $regexp, $matches)) {
		/* do something */
	}
}

> 
> Calgary, AB  T2A6C1
> Toronto, ON     T4M 0B0
> Saint John,  NB    E2L 4L1
> 
> ...such that it splits each line into City, Province and Postalcode
> (irrespective of occasional white space), e.g.:
> 
> Array
> (
>     [city]    => "Calgary",
>     [prov]    => "AB",
>     [postal]    => "T2A 6C1"
> )
> 
> Array
> (
>     [city]    => "Toronto",
>     [prov]    => "ON",
>     [postal]=> "T4M 0B0"
> )
> 
> Array
> (
>     [city]    => "Saint John",
>     [prov]    => "NB",
>     [postal]=> "E2L 4L1"
> )
> 

-- 
PHP General Mailing List (http://www.php.net/)
To unsubscribe, visit: http://www.php.net/unsub.php


[Index of Archives]     [PHP Home]     [Apache Users]     [PHP on Windows]     [Kernel Newbies]     [PHP Install]     [PHP Classes]     [Pear]     [Postgresql]     [Postgresql PHP]     [PHP on Windows]     [PHP Database Programming]     [PHP SOAP]

  Powered by Linux