views:

37

answers:

3

Hi this is my keyword

BC1024 , AB124 , CBC2548 ,

using preg match to separate the number and character ,

For that i tried this preg match expression , but its not working greatly ,

preg_match_all('/(?P\w+): (?P\d+)/', $flight_code_no, $matches,PREG_PATTERN_ORDER);

i want output as

Array
(
    [0] => BC1024: 2008
    [name] => BC
    [1] => BC
    [digit] => 1024
    [2] => 1024
)

But my preg match not working....

just shoing empty array,

+1  A: 

Running

<?php

foreach (array("BC1024", "AB124","CBC2548") as $v) {
  preg_match_all("/(?P<name>[A-Z]+)(?P<digit>\d+)/", $v, $matches); 
  var_dump($matches);
}

outputs (some newlines removed for brewity)

array(5) {
  [0]=>       array(1) {    [0]=>    string(6) "BC1024"  }
  ["name"]=>  array(1) {    [0]=>    string(2) "BC"  }
  [1]=>       array(1) {    [0]=>    string(2) "BC"  }
  ["digit"]=> array(1) {    [0]=>    string(4) "1024"  }
  [2]=>       array(1) {    [0]=>    string(4) "1024"  }
}
array(5) {
  [0]=>       array(1) {    [0]=>    string(5) "AB124"  }
  ["name"]=>  array(1) {    [0]=>    string(2) "AB"  }
  [1]=>       array(1) {    [0]=>    string(2) "AB"  }
  ["digit"]=> array(1) {    [0]=>    string(3) "124"  }
  [2]=>       array(1) {    [0]=>    string(3) "124"  }
}
array(5) {
  [0]=>       array(1) {    [0]=>    string(7) "CBC2548"  }
  ["name"]=>  array(1) {    [0]=>    string(3) "CBC"  }
  [1]=>       array(1) {    [0]=>    string(3) "CBC"  }
  ["digit"]=> array(1) {    [0]=>    string(4) "2548"  }
  [2]=>       array(1) {    [0]=>    string(4) "2548"  }
}
Maerlyn
+1  A: 
preg_match('/(?<name>[A-Z]+)(?<digit>\d+)/', 'BC1024', $result);

This gives

Array
(
    [0] => BC1024
    [name] => BC
    [1] => BC
    [digit] => 1024
    [2] => 1024
)
Artefacto
+2  A: 

Just to show that you don't need preg_match to split a simple string like that:

list($name,$digit) = sscanf($flight_code_no,'%[A-Z]%d');
Mark Baker