0

我有一个这样的字符串:

Land of gray [example]here is an example[/example] and pink.

我正在努力获取将[example]标签内容返回到变量中的 PHP/正则表达式代码......

4

2 回答 2

2

我不是 PHP 专家,但是……这个正则表达式可以工作

\[example\]([^\[]*)\[

这将捕获捕获组中的内容。

所以你的示例内容应该在 $matches[1] ???

前任:

<?php
$subject = "Land of gray [example]here is an example[/example] and pink.";
$pattern = '/\[example\]([^\[]*)\[/';
preg_match($pattern, $subject, $matches);
print_r($matches[1]);
?>

我没有测试上面的代码,因为我没有在这台机器上运行 PHP,但我认为这会工作......

于 2011-02-10T11:05:23.627 回答
0

BB 码允许属性。[url="somelink"]click[/url] 不会被这个正则表达式正确解析。我想给你一个答案,但是我的正则表达式不能很好地工作,这就是我首先来到这里的原因。哈哈。;-)

于 2012-09-24T20:10:17.790 回答