简体   繁体   English

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

[英]Extract number & text from string

I have this string: 我有这个字符串:

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

I need extract number & text from the string. 我需要从字符串中提取数字和文本。 The result should: 结果应为:

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

I tried to use this regex: 我试图使用此正则表达式:

\d+\s\/items

But it didn't work. 但这没有用。

You should be using preg_match_all : 您应该使用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);

Would output: 将输出:

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