Page 1 of 1

seperating text in different parts

Posted: Mon Oct 18, 2004 4:02 pm
by hward
feyd | Help us, help you. Please use

Code: Select all

and

Code: Select all

tags where approriate when posting code. Read:  [url=http://forums.devnetwork.net/viewtopic.php?t=21171]Posting Code in the Forums[/url][/color]


I'm not really sure what to ask how to do but this is what i am wanting to do.  I have a field i want to post to a php page that will be $city and it will have     City St  Zip    in it and i want to break those up into  $address1, $address2,$address3   to add them to the database    

for example

Code: Select all

$city = "Dodge City KS 67801"
and i want to change it to

Code: Select all

$address1 = "Dodge City"
$address2 = "KS"
$address3 = "67801"

feyd | Help us, help you. Please use

Code: Select all

and

Code: Select all

tags where approriate when posting code. Read:  [url=http://forums.devnetwork.net/viewtopic.php?t=21171]Posting Code in the Forums[/url][/color]

Posted: Mon Oct 18, 2004 4:48 pm
by hward

Code: Select all

<?php

$postcode = substr("$city", -5);    
$address2 = substr("$city", -9, -6);    
$address1 = substr("$city", -30, -9); 

?>

this seems to be working anything wrong with doing it like this

Posted: Mon Oct 18, 2004 4:48 pm
by feyd
regular expressions are generally the way to go with stuff like this.

Code: Select all

&lt;?php

	$text = 'Dodge City KS 67801';
	$ar = preg_split('#^\s*(&#1111;a-z ,''"-]+)&#1111;,\s]+((west|east|north|south|new)?\s*&#1111;a-z]{2,})&#1111;,\s]+(\d{5,9}(&#1111; -]\d{4})?)\s*$#iS', $text, -1, PREG_SPLIT_DELIM_CAPTURE | PREG_SPLIT_NO_EMPTY);
	var_export($ar);

?&gt;

Code: Select all

array (
  0 =&gt; 'Dodge City',
  1 =&gt; 'KS',
  3 =&gt; '67801',
)

Nifty neat

Posted: Mon Oct 18, 2004 5:19 pm
by neophyte
That's awesome Feyd. That line will even separate: Clevland OH 34567
Very cool!