Important alert: (current site time 7/15/2013 10:58:25 AM EDT)
 

VB icon

Address Splitter Address Cutter Address Seperator

Email
Submitted on: 11/30/2005 7:40:46 AM
By: Nathan A. Huebner  
Level: Beginner
User Rating: Unrated
Compatibility: PHP 3.0, PHP 4.0
Views: 7468
author picture
(About the author)
 
     If you have an address, that you get from a Customer, etc, and you need to put it into two or three or more fields, because it's too long, you can use this to split it, without corrupting the lines.
 
Can't Copy and Paste this?
Click here for a copy-and-paste friendly version of this code!
//**************************************
// for :Address Splitter Address Cutter Address Seperator
//**************************************
Do not retail. Free for non-profit use. Feel free to give it to people, but leave the credits.
code:
Can't Copy and Paste this?
Click here for a copy-and-paste friendly version of this code!
 
Terms of Agreement:   
By using this code, you agree to the following terms...   
  1. You may use this code in your own programs (and may compile it into a program and distribute it in compiled format for languages that allow it) freely and with no charge.
  2. You MAY NOT redistribute this code (for example to a web site) without written permission from the original author. Failure to do so is a violation of copyright laws.   
  3. You may link to this code from another website, but ONLY if it is not wrapped in a frame. 
  4. You will abide by any additional copyright restrictions which the author may have placed in the code or code's description.
				
//**************************************
// Name: Address Splitter Address Cutter Address Seperator
// Description:If you have an address, that you get from a Customer, etc, and you need to put it into two or three or more fields, because it's too long, you can use this to split it, without corrupting the lines.
// By: Nathan A. Huebner
//
// Inputs:An address... like 123 Bird Lane
//
// Returns:An address, split into an array, but safely split, and you designate the safety character, as well as the max characters per array line.
//
// Assumes:The code is mainly for shipping address modifications, to work with your Post Office, or Vendor. This is not a miracle solution, it's just a way to split an Address into pieces safely.
//
// Side Effects:If your address is much longer than the parts you have setup, on the very last part, if it's too long, and obviously you're on the last part, so it will just cut the leftover data, so your address is 100% clean, and compliant to your MaxParts and MaxCharacters
//
//This code is copyrighted and has// limited warranties.Please see http://www.Planet-Source-Code.com/vb/scripts/ShowCode.asp?txtCodeId=1859&lngWId=8//for details.//**************************************

<?
# TO USE THIS:
$address1="1234 Bird Road";
$address2="BOX 19, APT 12, Street 17, Corner 80";
$splitaddress = SplitAddress($address1,$address2);
print_r($splitaddress);
# BY NATHAN HUEBNER
function SplitAddress($addr1, $addr2) {
$address = trim($addr1 . " " . $addr2);
$maxchars=25;
$maxparts=4; 
$charfind=" ";
if (strlen($address)<=$maxchars) {
$part[1]=$address; # The address is fine.
} ELSE {
# The address needs to be split into parts.. It's longer than what you want it to be for 1 part
if ($maxparts > 1) {
$safespace = SafeSpace(substr($address,0,$maxchars),$charfind,$maxchars);
	if ($safespace>0) {
	$part[1]=trim(substr($address,0,$safespace));
	$part[2]=trim(substr($address,$safespace));
		if ($maxparts>2) {
		if (strlen($part[2])>$maxchars) {
		$safespace = SafeSpace(substr($part[2],0,$maxchars),$charfind,$maxchars);
	
			
			if ($safespace>0) {
			$part[3]=trim(substr($part[2],$safespace));
			$part[2]=trim(substr($part[2],0,$safespace));
	
			if ($maxparts>3) {
			if (strlen($part[3])>$maxchars) {
			$safespace = SafeSpace(substr($part[3],0,$maxchars),$charfind,$maxchars);
	
				if ($safespace>0) {
				$part[4]=trim(substr($part[3],$safespace));
				$part[3]=trim(substr($part[3],0,$safespace));
				}
			}
			}
			}
		}	 
		}
	}
}
}
# Cut the very last part...
if (strlen($part[$maxparts])>$maxchars) { $part[$maxparts]=substr($part[$maxparts],0,$maxchars); }
return $part;
}
# SafeSpace function finds the last best space to split the string
function SafeSpace($string,$charfind,$maxchars) {
$occount=substr_count($string,$charfind);
if ($occount==0) { return 0; } # No safe spaces found...
		$safespace=strrpos($string, $charfind);
	
		if ($safespace>$maxchars) {
		
		# Ok ... Lets dig in here to find a good & safe space :)
		
		$cut = substr($string,0,($safespace-1)); # Set to go right to the safespace position...
		$foundsafe=false; # init false...
		
			for ($s=0; $s < $occount; $s++) {
			# It will go through the occurrence counter to be safe!
			$lastoc=strrpos($cut,$charfind);
				if ($lastoc>$maxchars) {
				# Keep cutting
				$cut = substr($cut,0,($lastoc-1)); 		
				} ELSE {
				# Found a safe space...
				$foundsafe=true; # set to true, we did find a safe space...
				$safespace=$lastoc;
				break;
				}
			} # for $s
			
			if ($foundsafe==true) {
			# Found a safe space
			return $safespace;
			
			} ELSE {
			# Did not find a safe space...
			# I guess the next move here would be to just cut the data at the end regardless.
			# Or run it through a loop with non-identifier chars, such as special characters
			# perhaps it will run into a dash, slash, or parenthesis...
			return 0;
			}
		
		} ELSE {
		# Ok this safe space is safe, no need to cut it up...
		
		return $safespace;
		
		} # Check if the first occurrence is a good safe space..
		
} # SAFE SPACE FUNCTION
?>


Other 7 submission(s) by this author

 


Report Bad Submission
Use this form to tell us if this entry should be deleted (i.e contains no code, is a virus, etc.).
This submission should be removed because:

Your Vote

What do you think of this code (in the Beginner category)?
(The code with your highest vote will win this month's coding contest!)
Excellent  Good  Average  Below Average  Poor (See voting log ...)
 

Other User Comments

 There are no comments on this submission.
 

Add Your Feedback
Your feedback will be posted below and an email sent to the author. Please remember that the author was kind enough to share this with you, so any criticisms must be stated politely, or they will be deleted. (For feedback not related to this particular code, please click here instead.)
 

To post feedback, first please login.