繁体   English   中英

正则表达式/ BBCode解析器以查找两个字符串之间的字符串(两个字符串之间)并将其替换

[英]Regex / BBCode Parser to find string between two strings (Between two strings) and replace it

抱歉,我无法拿出更好的头衔。 基本上,我们将具有如下所示的文本:

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]

因此,我们要做的是获取引号内的所有图像,并将这些[img]标记替换为[url=http://www.url.to.image.jpg]Click here to view the image[/url] 但是此操作仅应在引号标签内的图像上发生。 我已经查看了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] [img] 这可以通过不使用.*?来完成.*? ,但((?!\\[/quote]).)*

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

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM