简体   繁体   中英

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 :

$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)

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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