English 中文(简体)
序 言
原标题:pregmatch separate number and character

BC1024 , AB124 , CBC2548 ,

采用序号,将编号和特性分开,

为此,我尝试了这一先令的表达方式,但并未开展大量工作。

preg_match_all( /(?Pw+): (?Pd+)/ , $flight_code_no, $matches,PREG_PATTERN_ORDER);

i 想要产出

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

但是,我的预言不行......。

只是空洞阵列,

最佳回答
preg_match( /(?<name>[A-Z]+)(?<digit>d+)/ ,  BC1024 , $result);

说明

Array
(
    [0] => BC1024
    [name] => BC
    [1] => BC
    [digit] => 1024
    [2] => 1024
)
问题回答

仅表明,你不需要先做事,才能把简单地描述成:

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

驾驶

<?php

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

产出(一些新路线改写)

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"  }
}




相关问题
Brute-force/DoS prevention in PHP [closed]

I am trying to write a script to prevent brute-force login attempts in a website I m building. The logic goes something like this: User sends login information. Check if username and password is ...

please can anyone check this while loop and if condition

<?php $con=mysql_connect("localhost","mts","mts"); if(!con) { die( unable to connect . mysql_error()); } mysql_select_db("mts",$con); /* date_default_timezone_set ("Asia/Calcutta"); $date = ...

定值美元

如何确认来自正确来源的数字。

Generating a drop down list of timezones with PHP

Most sites need some way to show the dates on the site in the users preferred timezone. Below are two lists that I found and then one method using the built in PHP DateTime class in PHP 5. I need ...

Text as watermarking in PHP

I want to create text as a watermark for an image. the water mark should have the following properties front: Impact color: white opacity: 31% Font style: regular, bold Bevel and Emboss size: 30 ...

How does php cast boolean variables?

How does php cast boolean variables? I was trying to save a boolean value to an array: $result["Users"]["is_login"] = true; but when I use debug the is_login value is blank. and when I do ...

热门标签