4

I've been doing research into this and I believe the answer is do to with regular expressions but I just can't get my head around them.

I have a number of strings and I need to select a number between two characters. Here is an example string

&user18339=18339,20070103,175439,pmt,793,A/3/1/2,335,793,A/3/1/2,

I need the number that occurs after A/3/1/2, and before the following ,

In this example I need to select 335. I can do this using explode however I run into problems when I need to get more than one number from a string, like in the example below.

Here is another example string

&user31097=31097,20070105,092612,pmt,4190,A/3/1/2,142,1162,A/3/1/1,22,2874,A/3/1/2,1046,4622,A/3/1/2,25,2872,A/3/1/2,

Again I need to get the numbers after the A/3/1/2, and before the following ,. So in this example I would want to take 142, 1046 and 25.

If anyone could let me know how to do this it would be greatly appreciated.

3
  • You have several occurrences of "A/3/1/2" in your examples, and did not specify what to do with that. Commented Jan 4, 2011 at 11:17
  • Is it always the "integer" that follows an "A/#/#/#" sequence after the "pmt" string? (e.g. there could be 1, 2, n occurrences?) Commented Jan 4, 2011 at 11:17
  • Can you please give some semantic meaning to these values? Commented Jan 4, 2011 at 11:19

3 Answers 3

3
$string = '&user31097=31097,20070105,092612,pmt,4190,A/3/1/2,142,1162,A/3/1/1,22,2874,A/3/1/2,1046,4622,A/3/1/2,25,2872,A/3/1/2,';
preg_match_all('/A\/3\/1\/2,([0-9]*?),/', $string, $matches);
var_dump($matches);
Sign up to request clarification or add additional context in comments.

1 Comment

@JWH89, If this answer works for you, you should check-mark it as answered. Also, please up-vote any answers that were helpful to you. If an answer was not helpful, comments -- as to why it didn't work for you -- help everyone figure out the best answer.
2
preg_match_all('/A\/3\/1\/2,([^,]+),/', $input, $matches = array());
print_r($matches);

1 Comment

[^,] will match anything until "," witch will match a string too , hes asking how to retrive the number not everithing .
1
if(preg_match_all('#A/3/1/2,([^,]*),#',$str,$matches)) {                        
        // $matches[1] will have the required results.
}

See it in action

1 Comment

[^,] will match anything until "," witch will match a string too , hes asking how to retrive the number not everithing .

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.