preg_match? Or something else?
am 03.04.2010 01:09:57 von Ashley
------=_NextPart_000_0001_01CAD287.58FBB7F0
Content-Type: text/plain;
charset="us-ascii"
Content-Transfer-Encoding: 7bit
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\d+)(?P[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?
------=_NextPart_000_0001_01CAD287.58FBB7F0--
Re: preg_match? Or something else?
am 03.04.2010 01:40:12 von List Manager
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\d+)(?P[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!
$string = "73G 146C 311- 309.1C";
$arr = preg_split("/[\s]+/", $string);
print_r($arr);
foreach ( $arr AS $item ) {
preg_match('|^(?P\d+)\.?(?P\d*)(?P[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
Re: preg_match? Or something else?
am 03.04.2010 01:48:38 von Nathan Rixham
Jim Lucas wrote:
> Ashley M. Kirchner wrote:
>> I have an array that's created as follows:
>>
>>
>>
>> $string = "73G 146C 311- 309.1C";
>>
>>
>> Anyone want to take a stab at it?
>>
>>
>
> Conditionals are your friend!
>
>
>
> $string = "73G 146C 311- 309.1C";
>
> $arr = preg_split("/[\s]+/", $string);
>
> print_r($arr);
>
> foreach ( $arr AS $item ) {
> preg_match('|^(?P\d+)\.?(?P\d*)(?P[A-Z-])|',
> $item,
> $matches);
>
> print_r($matches);
> }
>
> ?>
>
or w/ preg_match_all:
$regex = '/(([0-9]+)([^0-9])((?:[0-9]|\s+)))/';
$string = "73G 146C 311- 309.1C";
preg_match_all( $regex , $string , $matches );
print_r( $matches )
--
PHP General Mailing List (http://www.php.net/)
To unsubscribe, visit: http://www.php.net/unsub.php