排除除 preg_match 之外的所有内容

exclude all except from preg_match

如果我有这样的字符串

$string = 'the grey fox [[function name]] jumped over the moon';

如何删除字符串 除了 方括号 中的所有内容?我可以 find and replace 方括号内的代码,如下所示:

$page_function = preg_replace('#\[\[(.*?)\]\]#', '', $string);

我需要一种方法 inverse the preg_replace 以便我可以替换 preg_replace.

中找到的代码之外的所有代码

谢谢!

你最想找的是补函数 preg_match http://php.net/manual/en/function.preg-match.php

交互式示例 shell:

php > $string = 'the grey fox [[function name]] jumped over the moon';
php > preg_match('#\[\[(.*?)\]\]#', $string, $m);
php > var_dump($m);
array(2) {
  [0]=>
  string(17) "[[function name]]"
  [1]=>
  string(13) "function name"
}