[PHP] preg_match? Or something else?

2010-04-02 Thread Ashley M. Kirchner
I have an array that's created as follows:

 

$string = 73G146C   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(/^(?Plocation\d+)(?Pletter[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?



Re: [PHP] preg_match? Or something else?

2010-04-02 Thread Jim Lucas
Ashley M. Kirchner wrote:
 I have an array that's created as follows:
 
  
 
 $string = 73G146C   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(/^(?Plocation\d+)(?Pletter[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 = 73G146C   311- 309.1C;

$arr = preg_split(/[\s]+/, $string);

print_r($arr);

foreach ( $arr AS $item ) {
preg_match('|^(?Plocation\d+)\.?(?Pdecimal\d*)(?Pletter[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: [PHP] preg_match? Or something else?

2010-04-02 Thread Nathan Rixham
Jim Lucas wrote:
 Ashley M. Kirchner wrote:
 I have an array that's created as follows:

  

 $string = 73G146C   311- 309.1C;


 Anyone want to take a stab at it?


 
 Conditionals are your friend!
 
 plaintext?php
 
 $string = 73G146C   311- 309.1C;
 
 $arr = preg_split(/[\s]+/, $string);
 
 print_r($arr);
 
 foreach ( $arr AS $item ) {
   preg_match('|^(?Plocation\d+)\.?(?Pdecimal\d*)(?Pletter[A-Z-])|',
$item,
$matches);
 
   print_r($matches);
 }
 
 ?
 

or w/ preg_match_all:

?php
$regex = '/(([0-9]+)([^0-9])((?:[0-9]|\s+)))/';
$string = 73G146C   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