【发布时间】:2018-06-02 04:04:42
【问题描述】:
我在编写正确的正则表达式时遇到问题。
我在我的系统中使用短代码,它们运行良好。我已经对它的属性等进行了排序,但现在我想在另一个简码中使用一个简码。
这是我准备正则表达式的方法:
$attributes_regexp = "([^\]]*?)";
$inner_content_regexp = "(.*?)";
$flags_regexp = "im";
$regexp = "/\[$shortcode$attributes_regexp\]$inner_content_regexp\[\/$shortcode\]/$flags_regexp";
preg_match_all($regexp, $content, $found_occurrences);
下面是一个准备好的正则表达式的例子:
\[file([^\]]*?)\](.*?)\[\/file\]
这里有一点需要分析的 HTML:
<div class="row">
<div class="col-md-8">
<h2>Test page</h2>
<p> </p>
<p><strong>Some</strong> content</p>
<p>Lorem ipsum dolor. </p>
<p>Dolor sit amet.</p>
<p>[file id=290 type=link][file id=283 type=image width=100 height=100][/file][/file]</p>
</div>
<div class="col-md-3 offset-md-1">
<p>[file id=289 type=image][/file]</p>
</div>
</div>
问题是它只正确地把最后一个更改为图像,但前一个是像
[file id=290 type=link][file id=283 type=image width=100 height=100][/file]
而不是两个独立的
[文件 id=283 类型=图像宽度=100 高度=100][/file]
和
[文件 id=290 type=link][/file]
任何想法如何排序?
非常感谢, 托马斯
【问题讨论】:
-
所以您正在使用正则表达式将
[file id=289 type=image][/file]之类的内容替换为具有适当属性的 img 标签? -
是的,而且效果很好。但现在我需要在另一个里面替换一个。问题是它们都是一样的。
标签: php regex shortcode regular-language bbcode