-1

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.

5
  • 1
    So for what did you put \/ in there? Commented Jun 30, 2016 at 3:00
  • Where is the 10 and 30 coming from? Database? Variable name? value is static? Commented Jun 30, 2016 at 3:01
  • @Levi Does it make a difference? Commented Jun 30, 2016 at 3:03
  • Yes... You have to get the logic of what you're trying to do correct else its not going to function the way you want it too. Commented Jun 30, 2016 at 3:04
  • 1
    @Levi I think you missed that the values are coming from parsing a string. Commented Jun 30, 2016 at 3:11

1 Answer 1

2

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)
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.