-1

Here is my string:

$string = 'This the s number [:[123084]:] and [:[123085]:] 12374 [:[123087]:]';

How to get number if number is to [:[xxxx]:]this case? I want to get number(xxxx) from this case as array.

I want the result something like this:

$array = array( 123084, 123085, 123087);
Tushar
  • 118
  • 11

1 Answers1

1

This does the job:

$string = 'This the s number [:[123084]:] and [:[123085]:] 12374[:[123087]:]';
preg_match_all('/(?<=\[:\[)\d+(?=\]:\])/', $string, $matches);
print_r($matches);

Output:

Array
(
    [0] => Array
        (
            [0] => 123084
            [1] => 123085
            [2] => 123087
        )
)

Explanation:

/           : regex delimiter
  (?<=      : lookbehind, zero length assertion, make sure we have the following BEFORE the current position
    \[:\[   : literally [:[
  )         : end lookbehind
  \d+       : 1 or more digits
  (?=       : lookahead, zero length assertion, make sure we have the following AFTER the current position
    \]:\]   : literally ]:]
  )         : end lookahead
/           : regex delimiter

You will find usefull informations here

Toto
  • 89,455
  • 62
  • 89
  • 125