代码之家  ›  专栏  ›  技术社区  ›  Valentin Flachsel

在PHP中对字母数字字符串使用preg_split

  •  0
  • Valentin Flachsel  · 技术社区  · 15 年前

    考虑字符串 12345aaa . 我想用 preg_split() 因此只返回其数字部分(即 12345 )。我该如何编写regex呢?

    1 回复  |  直到 12 年前
        1
  •  5
  •   Yacoby    15 年前

    你想要的功能不是preg-split preg_match 因为您不想将一个字符串拆分为多个部分,而是要检索其中的一部分。因为我不知道更多的细节,我只能提供一个如何做的粗略的例子。

    $reg = null;
    //match the first set of 1 or more numbers
    if ( preg_match('/\d+/', $str, $reg) ){
        $num = $reg[0];
    }