繁体   English   中英

从字符串中提取数字和文本

[英]Extract number & text from string

我有这个字符串:

There 10 items in shop A,
There 30 items in shop B.

我需要从字符串中提取数字和文本。 结果应为:

array(
0 => 10 items,
1 => 30 items
);

我试图使用此正则表达式:

\d+\s\/items

但这没有用。

您应该使用preg_match_all

$string = 'There 10 items in shop A, There 30 items in shop B.';
$matches = null;
preg_match_all('/\d+\sitems/', $string, $matches);
var_dump($matches);

将输出:

array (size=1)
  0 => 
    array (size=2)
      0 => string '10 items' (length=8)
      1 => string '30 items' (length=8)

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM