Ashley M. Kirchner wrote:
> I have an array that's created as follows:
>
>
>
> $string = "73G 146C 311- 309.1C";
>
> $arr = preg_split("/[\s]+/", $string);
>
>
>
> Now I need to take each element in that array, and break them up even
> further so that I get:
>
>
>
> 73G => "73" and "G"
>
> 146C => "146" and "C"
>
> 311- => "311" and "-"
>
> 309.1C => "309", "1", and "C" (notice 3 elements here)
>
>
>
> I'm having a hard time trying to figure out what the proper regex would be
> for this, or whether that's the right thing to do. So far I've gotten this:
>
>
>
> preg_match("/^(?P<location>\d+)(?P<letter>[A-Z-])/", $item, $matches);
>
> print_r($matches);
>
>
>
> Which gives me:
>
>
>
> Array
>
> (
>
> [0] => 73G
>
> [location] => 73
>
> [1] => 73
>
> [letter] => G
>
> [2] => G
>
> )
>
> Array
>
> (
>
> [0] => 146C
>
> [location] => 146
>
> [1] => 146
>
> [letter] => C
>
> [2] => C
>
> )
>
> Array
>
> (
>
> [0] => 311-
>
> [location] => 311
>
> [1] => 311
>
> [letter] => -
>
> [2] => -
>
> )
>
> Array
>
> (
>
> )
>
> However that's as far as it goes. For the other number it returns an empty
> array and I know why, the decimal point. Now I can evaluate each $item
> every time, see if they contain a decimal point, and pass it to a different
> regex string, but that seems rather inefficient to me. So how can I do this
> all in one fell swoop?
>
> Anyone want to take a stab at it?
>
>
Conditionals are your friend!
<plaintext><?php
$string = "73G 146C 311- 309.1C";
$arr = preg_split("/[\s]+/", $string);
print_r($arr);
foreach ( $arr AS $item ) {
preg_match('|^(?P<location>\d+)\.?(?P<decimal>\d*)(?P<letter>[A-Z-])|',
$item,
$matches);
print_r($matches);
}
?>
--
Jim Lucas
NOC Manager
541-323-9113
BendTel, Inc.
http://www.bendtel.com
--
PHP General Mailing List (http://www.php.net/)
To unsubscribe, visit: http://www.php.net/unsub.php