preg_match? Or something else?

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!

<?php<br /> <br /> $string = "73G 146C 311- 309.1C";<br /> <br /> $arr = preg_split("/[\s]+/", $string);<br /> <br /> print_r($arr);<br /> <br /> foreach ( $arr AS $item ) {<br /> preg_match('|^(?P<location>\d+)\.?(?P<decimal>\d*)(?P<letter>[A-Z-])|',<br /> $item,<br /> $matches);<br /> <br /> print_r($matches);<br /> }<br /> <br /> ?><br /> <br /> -- <br /> Jim Lucas<br /> NOC Manager<br /> 541-323-9113<br /> BendTel, Inc.<br /> http://www.bendtel.com<br /> <br /> -- <br /> PHP General Mailing List (http://www.php.net/)<br /> To unsubscribe, visit: http://www.php.net/unsub.php</p> </article> <article> <h2>Re: preg_match? Or something else?</h2><span>am 03.04.2010 01:48:38 von Nathan Rixham</span> <p>Jim Lucas wrote:<br /> > Ashley M. Kirchner wrote:<br /> >> I have an array that's created as follows:<br /> >><br /> >> <br /> >><br /> >> $string = "73G 146C 311- 309.1C";<br /> >><br /> >><br /> >> Anyone want to take a stab at it?<br /> >><br /> >><br /> > <br /> > Conditionals are your friend!<br /> > <br /> > <plaintext><?php<br /> > <br /> > $string = "73G 146C 311- 309.1C";<br /> > <br /> > $arr = preg_split("/[\s]+/", $string);<br /> > <br /> > print_r($arr);<br /> > <br /> > foreach ( $arr AS $item ) {<br /> > preg_match('|^(?P<location>\d+)\.?(?P<decimal>\d*)(?P<letter>[A-Z-])|',<br /> > $item,<br /> > $matches);<br /> > <br /> > print_r($matches);<br /> > }<br /> > <br /> > ?><br /> > <br /> <br /> or w/ preg_match_all:<br /> <br /> <?php<br /> $regex = '/(([0-9]+)([^0-9])((?:[0-9]|\s+)))/';<br /> $string = "73G 146C 311- 309.1C";<br /> preg_match_all( $regex , $string , $matches );<br /> print_r( $matches )<br /> <br /> -- <br /> PHP General Mailing List (http://www.php.net/)<br /> To unsubscribe, visit: http://www.php.net/unsub.php</p> </article> <footer> <a href="/">Index</a> | <a href="/impressum.php">Impressum</a> | <a href="/datenschutz.php">Datenschutz</a> | <a href="https://www.xodox.de/">XODOX</a> </footer> </main> </body> </html>