PHP - select part of a line between two characters

I do this research and I believe that the answer is in regular expressions, but I just can't get around them.

I have several lines and I need to choose a number between two characters. Below is an example line

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

I need a number that happens after A/3/1/2, and before the next one ,

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

Here is another line example

 &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 A/3/1/2, and until the next,. Therefore, in this example, I would like to take 142 , 1046 and 25 .

If anyone can tell me how to do this, we will be very grateful.

+4
source share
3 answers
 $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); 
+3
source
 preg_match_all('/A\/3\/1\/2,([^,]+),/', $input, $matches = array()); print_r($matches); 
+2
source
 if(preg_match_all('#A/3/1/2,([^,]*),#',$str,$matches)) { // $matches[1] will have the required results. } 

Look in action

+1
source

Source: https://habr.com/ru/post/1334190/


All Articles