Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations IamaSherpa on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Stripping a string.

Status
Not open for further replies.
Jun 18, 2003
6
US
I'm trying to extract data from the string:
"FIPSCodeVinitaOK74301Craig40035"
in the order
"FIPSCode{City}{ST}{ZIP}{COUNTY}{FIPS}"
The data i want is the state initials (any two letters before the zip code), and the county (The text between the zip code and the numeric fips code).

Is there any way i can break this string into an array, such that it would be
$array[0] = "FIPSCode"
$array[1] = "Vinita"
$array[2] = "OK"
$array[3] = "74301"
$array[4] = "Craig"
$array[5] = "40035"

Any feedback at all is much appreciated, if you have any questions about my inquiry, you can reply, or message me at bradinthehouse on yahoo messenger.
 
Without knowing whether this is a fixed-width format or something else, I can only recommend regular expressions.

Code:
<?php

$string = 'FIPSCodeVinitaOK74301Craig40035';

preg_match_all ('/^(FIPSCode)([a-z]*)([a-z]{2})(\d{5})([a-z]*)(\d*)$/i', $string, $data_array);

print_r ($data_array);
print &quot;\n&quot;;
?>

Outputs:

Code:
Array
(
    [0] => Array
        (
            [0] => FIPSCodeVinitaOK74301Craig40035
        )

    [1] => Array
        (
            [0] => FIPSCode
        )

    [2] => Array
        (
            [0] => Vinita
        )

    [3] => Array
        (
            [0] => OK
        )

    [4] => Array
        (
            [0] => 74301
        )

    [5] => Array
        (
            [0] => Craig
        )

    [6] => Array
        (
            [0] => 40035
        )

)

Want the best answers? Ask the best questions: TANSTAAFL!!
 
Could array[0],array[1], or array[2] ever be a number?

If not, you could find the location of the first number and take the two previous letters:

for ($i=0;$i<strlen($string);$i++){
if (is_numeric(substr($string,$i,1)){
$state = substr($string,$i-2,2)
break;
}
}
 
Just some pseudo-thought. You know the last part (a FIPS?) is numbers (always 5 digits?). So start from the right using some kind of isnumber() or regex to grab the numbers. Take the resulting string, take all non-numbers from right-to-left. That's the county. Do the same thing again, but get the letters instead, and now you have the state (it's only two characters long, and consistant). Next, go from left-to-right and chop off the 'FIPSCode' part of the string, the result is your city.

Or if you can control the creation of the string, use something like a semicolon to seperate each piece of the string, then explode() the string in your code later.

Sorry if you already thought about this and it's a 'duh' answer. You can get better details in the string section of the php manual at:

----
JBR
 
What it comes down to is that if you know that the characters won't be numbers, and vice-versa, then sleipnir214's solution is your best bet. That gives you what you asked for. Mine just gave you the state. Besides, regular expressions are more efficient.
 
EXCELLENT, unfortunately there is more information after the FIPS number that could cause a problem. is there any way i can get all after the FIPS number to be either truncated or set in the array as well? e.g. here is the content of the string in reality,
FIPSCodeVinitaOK74301Craig40035
Search Again...For any valid ZIP code, find the ci...

Thanks for your help, all of you!

Brad
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top