-1

I'm hoping someone can help me. I would like a PHP function that takes a string (X), and searches it for another string (Y). After Y is found (left to right search), the function will return only numbers found after Y but will not return numbers (or anything after the search finds another non-numeric character in the string Y (to the right).

Example: Searching for "peas, "

String = 'Tomorrow we will eat peas, 10 times as many as we did on the 7th or 8th of this month.'

Returns 10

Can someone help me out please?

jonathanbell
  • 2,507
  • 5
  • 23
  • 40
  • 1
    Seems like a simple thing to do with a regular expression. What did you try? – Barmar Apr 11 '14 at 23:50
  • Seems like it. I'm at a bit of a loss. I'm not very good with regex. – jonathanbell Apr 11 '14 at 23:51
  • Or a combination of a regular expression and `strrchr()` – jtheman Apr 11 '14 at 23:52
  • How do you expect to become good at it if you don't try? You're not going to learn by just copying answers from the net. – Barmar Apr 11 '14 at 23:52
  • OK, sorry I asked... I'll try and find a solution with regex and/or two passes of strrpos() and post the answer back here for others to use. – jonathanbell Apr 11 '14 at 23:54
  • Ouch, a downvote... Well for the record, I don't post every single problem I have in code onto Stackoverflow. But occasionally I get stuck and seek help from others. I can learn via examples. Is the point of this site to only talk about programming theory? Sometimes, you have a problem in code or life and you ask Google. Today, I didn't ask Google enough apparently (although I did try). I thought I could ask this here on Stackoverflow. Jeesh. – jonathanbell Apr 12 '14 at 00:03
  • 'Ouch, a downvote.' @jonny.milano Don't sweat it, they happen. I imagine because your question doesn't demonstrate any research and comes across as a 'gimme da codez' question even though you probably didn't intend it that way. – vascowhite Apr 12 '14 at 00:07
  • @jonny.milano - don't worry about the downvote, some people are trigger happy and only scan through the question. – ScottMcGready Apr 12 '14 at 00:10

1 Answers1

1
$string =  'Tomorrow we will eat peas, 10 times as many as we did on the 7th or 8th of this month.';
$search = 'peas, ';
// Turn the search string into a regular expression
$regexp = '/' . preg_quote($search) . '(\d+)/';
preg_match($regexp, $string, $match);
$number = $match[1];
Barmar
  • 741,623
  • 53
  • 500
  • 612
  • "I get by with a little help from my friends." Thank you, @Barmar. – jonathanbell Apr 11 '14 at 23:58
  • 1
    Consider whether the number has to *immediately* follow the search string or not! Also, maybe you want to say `$number = intval($match[1])` or `$number = floatval($match[1])` at the end there... (?) – RobP Apr 11 '14 at 23:58
  • 1
    In his example, the number is immediately after the string (he even included the space after the comma in the search string), so I assumed that was the criteria. I don't see any need for `intval()`, as PHP will automatically convert numeric strings to the corresponding numbers when necessary. – Barmar Apr 12 '14 at 00:00