PHP正则表达式帮助(preg_replace)[关闭]

Lets say I have the string: "Test [[1294]] example"

How could I do a preg_replace() that would extract the number from the double brackets?

Could someone please advise me to what the (Greedy) expression for getting that number would be? -it'll always be a integer within double brackets.

Thanks alot.

You might want to check out a tutorial.

If you want to "extract" the number, there is no need for preg_replace. Use preg_match or preg_match_all (if there are multiple occurrences) instead:

preg_match('/\[\[(\d+)\]\]/', $input, $matches);
$integer = $matches[1];

or

preg_match_all('/\[\[(\d+)\]\]/', $input, $matches);
$integerArray = $matches[1];

If instead of "extract" you actually meant something like "how can I preg_replace this term and use the extracted integer number", you can use the same regular expression and refer to the captured integer, using $1:

$output = preg_replace('/\[\[(\d+)\]\]/', 'Found this integer -> $1 <-', $input);

Which would result in:

Test Found this integer -> 1294 <- example

You would use preg_match(), not preg_replace():

$subject = 'Test [[1294]] example';
preg_match('/\[\[(\d+)\]\]/', $subject, $match);
echo $match[1];