簡體   English   中英

從內容中刪除 Wordpress 標題短代碼,但使用 PHP 保留內容

[英]Removing Wordpress caption shortcodes from content, but keep content using 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('%(\[caption\b[^\]]*\](.*?)(\[\/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

如果 align 標簽是“aligncenter”,則m-auto標簽出現在哪里,但這確實是一個獎勵,如果我能在標題周圍獲得一個跨度,在標題數據周圍獲得一個 div,那就太好了。

最終堅持使用我的原始代碼。


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

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM