Question: I have the contents of a text file in a php string. Now I want to store the two characters which occur before the following strings - `"(A)","(B)","(C)","(B+)"`
For example if the `php` variable contains something like -

```
33(F) 15352(1) 24 31 55(B+) 15360(1) 6 32 38 70(A) 2 3 4 5 6 7 8 9 10
10
*Passed with Grace Marks
*SID:  Student  ID;                          SchemeID:  The  scheme  
applicable  to  the  student.
Date on which pdf made: 09/10/2018
RTSID: 2018100901520151640002

```

Then I want to store 33,70 in an array.
Note that I want to create a numeric array.

Answer: 
This is a much better answer than mine (by @Andreas):

```
    $re = '/(\d+)\(([A-Z]\+?)\)/m';
    $str = '33(F) 15352(1) 24 31 55(B+) 56(B+) 15360(1) 6 32 38 70(A) 2 3 4 5 6 7 8 9 10
    10
    *Passed with Grace Marks
    *SID:  Student  ID;                          SchemeID:  The  scheme  
    applicable  to  the  student.
    Date on which pdf made: 09/10/2018
    RTSID: 2018100901520151640002';

    preg_match_all($re, $str, $matches);
    $res = array_map(function($x, $y){
        return [$y, $x];
    },$matches[1], $matches[2]);
    print_r($res);

```

For one single input, this would work, and its not the best:

```
      function f(){
        $inputs = '33(F) 15352(1) 24 31 55(B+) 15360(1) 6 32 38 70(A) 2 3 4 5 6 7 8 9 10
        10
        *Passed with Grace Marks
        *SID:  Student  ID;                          SchemeID:  The  scheme  
        applicable  to  the  student.
        Date on which pdf made: 09/10/2018
        RTSID: 2018100901520151640002';

        $a=strpos($inputs,'(A)');
        $b=substr($inputs, $a-2,2);
        var_dump($b);
      }

    f();

```
Another option is to using [preg\_match\_all](http://php.net/manual/en/function.preg-match-all.php) and capture 1+ digits `\d+` (or 2 digits exactly `\d{2}` in a capturing group followed by matching an uppercase char followed by an optional plus sign `\([A-Z]\+?`

Then from the resulting array convert the values using [array\_map](http://php.net/manual/en/function.array-map.php) and [intval](http://php.net/manual/en/function.intval.php).

For example:

```
$re = '/(\d+)\([A-Z]\+?\)/';
$str = '33(F) 15352(1) 24 31 55(B+) 15360(1) 6 32 38 70(A) 2 3 4 5 6 7 8 9 10
10
*Passed with Grace Marks
*SID:  Student  ID;                          SchemeID:  The  scheme  
applicable  to  the  student.
Date on which pdf made: 09/10/2018
RTSID: 2018100901520151640002';

preg_match_all($re, $str, $matches);
var_dump(array_map('intval',$matches[1]));

```

Result

```
array(3) {
  [0]=>
  int(33)
  [1]=>
  int(55)
  [2]=>
  int(70)
}

```

[Php demo](https://3v4l.org/9vgZ5)