亚洲在线久爱草,狠狠天天香蕉网,天天搞日日干久草,伊人亚洲日本欧美

為了賬號安全,請及時綁定郵箱和手機立即綁定
已解決430363個問題,去搜搜看,總會有你想問的

在 PHP 中刪除 HTML 屬性

在 PHP 中刪除 HTML 屬性

PHP
至尊寶的傳說 2022-07-22 10:42:22
我有一個這樣的 HTML 代碼:<figure id="attachment_83" aria-describedby="caption-attachment-83" style="width: 300px" class="wp-caption alignnone">    <img class="size-medium wp-image-83" src="https://sampleonly.com/wp-content/uploads/2020/01/my-picture-300x169.jpg"        alt="" width="300" height="169"        srcset="https://sampleonly.com/wp-content/uploads/2020/01/my-picture-300x169.jpg 300w, https://sampleonly.com/wp-content/uploads/2020/01/my-picture-1024x576.jpg 1024w, https://sampleonly.com/wp-content/uploads/2020/01/my-picture-768x432.jpg 768w, https://sampleonly.com/wp-content/uploads/2020/01/my-picture-1536x864.jpg 1536w, https://sampleonly.com/wp-content/uploads/2020/01/my-picture-1218x685.jpg 1218w, https://sampleonly.com/wp-content/uploads/2020/01/my-picture.jpg 1920w"        sizes="(max-width: 300px) 100vw, 300px" />    <figcaption id="caption-attachment-83" class="wp-caption-text">This is my picture text.</figcaption><figure>我想刪除 id、style、class 等。最終代碼如下:<figure>    <img src="https://sampleonly.com/wp-content/uploads/2020/01/my-picture.jpg"/>    <figcaption>This is my picture text.</figcaption><figure>有人可以幫我嗎?謝謝你。
查看完整描述

2 回答

?
catspeake

TA貢獻1111條經驗 獲得超0個贊

這是 preg 替換,您可以使用它來刪除所需屬性以外的屬性。


$html = preg_replace("/(<img\\s)[^>]*(src=\\S+)[^>]*(\\/?>)/i", "$1$2$3", $html);

$html = preg_replace("/(<figure\\s)[^>]*[^>]*(\\/?>)/i", "$1$2$3", $html);

$html = preg_replace("/(<figcaption\\s)[^>]*[^>]*(\\/?>)/i", "$1$2$3", $html);

這會給你輸出


<figure ><img src="https://sampleonly.com/wp-content/uploads/2020/01/my-picture-300x169.jpg"><figcaption >This is my picture text.</figcaption><figure>



查看完整回答
反對 回復 2022-07-22
?
臨摹微笑

TA貢獻1982條經驗 獲得超2個贊

您可以使用以下代碼注意:如果您包含 jquery,則僅使用腳本,否則使用下面的 jquery 鏈接


<script src="https://ajax.googleapis.com/ajax/libs/jquery/3.4.1/jquery.min.js"></script>


    <script>

        $('figure,figcaption,img').removeAttr("id style class height width sizes   srcset aria-describedby");

    </script>


查看完整回答
反對 回復 2022-07-22
  • 2 回答
  • 0 關注
  • 214 瀏覽

添加回答

舉報

0/150
提交
取消
微信客服

購課補貼
聯系客服咨詢優惠詳情

幫助反饋 APP下載

慕課網APP
您的移動學習伙伴

公眾號

掃描二維碼
關注慕課網微信公眾號