BC1024 , AB124 , CBC2548 ,
using preg match to separate the number and character ,
For that 开发者_JAVA百科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,
preg_match('/(?<name>[A-Z]+)(?<digit>\d+)/', 'BC1024', $result);
This gives
Array ( [0] => BC1024 [name] => BC [1] => BC [digit] => 1024 [2] => 1024 )
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');
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" }
}
精彩评论