0

这是字符串:

$text = "aaaaaaaa[[Image:1939.jpg||thumb|right|200px|[[1939]], [[Mr. X]] is [[here]].]]bbb";

我想得到这个:

Image:1939.jpg||thumb|right|200px|[[1939]],[[Mr. X]] is [[here]].

这是一种 mediawiki 标记格式。一篇文章有​​一个或多个图像标记。

我的代码:

$pattern = "/\[\[Image:([\s\S]*?)\]\]/";

preg_match($pattern, $text, $match);

但我得到了

Image:1939.jpg||thumb|right|200px|[[1939

请帮忙!

4

3 回答 3

1

您可以使用递归模式来做到这一点:

$pattern = '~\[\[((?>[^[\]]++|(?R))*+)]]~';
$subject = 'aaaaaaaa[[Image:1939.jpg||thumb|right|200px|[[1939]], [[Mr. X]] is [[here]].]]bbb';

preg_match($pattern, $subject, $match);

echo '<pre>' . print_r($match[1], true);

解释:

$pattern =
  '~               # delimiter of the pattern
   \[\[            # the two open square brackets
   (               # first capture group
     (?>           # atomic group
         [^[\]]++  # all chars except square brackets 1 or more time
       |           # OR
         (?R)      # recurse the whole pattern
     )*+           # end of atomic group 0 or more time (allow void brackets)
   )               # end of capture group
   ]]              # the two closing square brackets
   ~x';            // delimiter with the x modifier that allow comments
于 2013-05-11T00:55:26.997 回答
0
$string = "[[Image:1939.jpg||thumb|right|200px|[[1939]],[[Mr. X]] is [[here]].]]";
$pattern = '/\[\[(.*)\]\]/';

preg_match($pattern, $string, $result);

var_dump($result);
于 2013-05-10T11:34:36.350 回答
0

在你的所有条件下试试这个

$text = "aaaadsfasdfaaaa[[Image:1939.jpg||thumb|right|200px|[[1939]],[[Mr. X]] is [[here]].]]bbbbdwebadfa";
$pattern = "/^[^.]+\[\[(.*)\]\]+[^.]+$/";
preg_match($pattern, $text, $match);
echo $match[1];
于 2013-05-10T12:02:44.640 回答