Hi,
I'm having some trouble figuring out a way to assign my regex pattern matches to an array, such that each match is saved in the index of an array. Here's the deal:
I have a string that can have one or more state abbreviations, delimited by spaces:
$string = "AK AL NV WA";
Here's my regular expression for a state abbreviation:
[a-zA-Z]{2}
Ultimately, what I want are each of my state abbreviations to be in their own array element, such that (in pseudo terms):
$array[0] holds "AK"
$array[1] holds "AL"
$array[2] holds "NV"
$array[3] holds "WA"
Here's what I've got so far:
$string = "AK AL NV WA";
$temp_array = array();
$final_array = array();
if (preg_match_all("([a-zA-Z]{2})", $string, $matches, PREG_PATTERN_ORDER)){
$matches_max = count($matches[0]);
for ($k=0; $k<$matches_max; $k++){
$temp_array[] = $regs[0][$k];
}
}
//Copy the data from $temp_array to $final_array
$temp_array_max = count($temp_array);
for ($x=0; $x<$temp_array_max; $x++){
$final_array[$x] = $temp_array[$x];
}
This is producing funny results... does this look right? I really appreciate any assistance! 🙂