Php Regex/BBCode解析器查找两个字符串之间的字符串(两个字符串之间)并替换它

Php Regex/BBCode解析器查找两个字符串之间的字符串(两个字符串之间)并替换它,php,regex,Php,Regex,对不起,我想不出更好的标题了。基本上,我们将有这样的文本: Wow, thanks for that image. It really helps! [quote]Here, this image may help you. [img]http://www.url.to.image.jpg[/img] [/quote] 文本也可以显示为 Wow, thanks for that image. It really helps! [quote="username"]Here, th

对不起,我想不出更好的标题了。基本上,我们将有这样的文本:

Wow, thanks for that image. It really helps!  
[quote]Here, this image may help you.  
[img]http://www.url.to.image.jpg[/img]  
[/quote]
文本也可以显示为

Wow, thanks for that image. It really helps!  
[quote="username"]Here, this image may help you.  
[img]http://www.url.to.image.jpg[/img]  
[/quote]
所以,我们想要做的是抓取报价中的任何图像,并用[url]替换这些[img]标记=http://www.url.to.image.jpg]单击此处查看图像[/url]。但此操作只应发生在quote标记内的图像上。我已经查看了PHP的各种BBCode解析器,但找不到任何能够做到这一点的工具,而且我不确定执行此任务所需的正则表达式。

您可以尝试:

(\[quote[^]]*].*?)(?=\[img])\[img](.*?)\[/img]
带替换字符串

$1[url=$2]Click here to view the image[/url]
我不确定代码。也许:

$result = preg_replace('%(\[quote[^]]*].*?)(?=\[img])\[img](.*?)\[/img]%sim', '$1[url=$2]Click here to view the image[/url]', $subject);

我们必须确保在[img]之前没有[quote]。这可以通过不使用。*?来实现,但是\[quote]*

$regex = '#(\[quote[^]]*]((?!\[/quote]).)*)\[img](.*?)\[/img]#s';
$replace = '$1[url=$3]Click here to view the image[/url]';
$str = preg_replace($regex, $replace, $str);