从内容中删除Wordpress标题短代码,但保留内容使用PHP



在将数千篇文章从Wordpress迁移到Laravel环境后,我已经设法减轻了大多数灾难,但我留下了帖子中标题短代码的小问题。它们是这样的:

[caption id="attachment_38469" align="alignnone" width="1042"]<a href="https://uq.wd3.example.com/R-05591-1"><img class="size-full wp-image-38469" src="https://example.wpengine.com/wp-content/uploads/2022/01/U0036.jpg" alt="This is an example" width="1042" height="2500" /></a> This is where the caption would go[/caption]
到目前为止,我已经设法剥离标题标签(甚至多个标题)与以下代码:
$caption = 'Beginning stuff [caption id="attachment_38469" align="alignnone" width="1042"]<a href="https://uq.wd3.example.com/R-05591-1"><img class="size-full wp-image-38469" src="https://example.wpengine.com/wp-content/uploads/2022/01/U0036.jpg" alt="This is an example" width="1042" height="2500" /></a> This is where the caption would go[/caption] ending stuff [caption id="attachment_38469" align="alignnone" width="1042"]<a href="https://uq.wd3.example.com/R-05591-1"><img class="size-full wp-image-38469" src="https://example.wpengine.com/wp-content/uploads/2022/01/U0036.jpg" alt="This is an example" width="1042" height="2500" /></a> Second caption[/caption] and a last spot';
$c2 = preg_replace('%([captionb[^]]*](.*?)([/caption]))%', '$2', $caption);
// Preview:
print_r($c2, true);

理想情况下,我想把这些标题变成:

Content before<div class="captioned-image m-auto"><a href="*"><img src="*" /></a><span class="caption">*</span></div>content after

m-auto标签出现的地方,如果align标签是& aligncenter",但这真的是一个额外的好处,如果我能在标题周围有一个span和一个div在标题数据周围,那就太好了。

最后还是坚持原来的代码。


$sanitised_content = preg_replace('%([captionb[^]]*](.*?)([/caption]))%', '$2', $content);

最新更新