正在删除 PHP 中的 HTML 属性
Removing HTML atribute in PHP
我有这样的 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>
这里有人可以帮助我吗?先谢谢了
这是 preg 替换,您可以使用它来删除所需属性以外的属性。
$html = preg_replace("/(<img\s)[^>]*(src=\S+)[^>]*(\/?>)/i", "", $html);
$html = preg_replace("/(<figure\s)[^>]*[^>]*(\/?>)/i", "", $html);
$html = preg_replace("/(<figcaption\s)[^>]*[^>]*(\/?>)/i", "", $html);
这将为您提供输出
<figure ><img src="https://sampleonly.com/wp-content/uploads/2020/01/my-picture-300x169.jpg"><figcaption >This is my picture text.</figcaption><figure>
您可以使用下面的代码
注意:如果您包含 jquery 则仅使用脚本,否则使用下面的 jquery link
<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>
我有这样的 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>
这里有人可以帮助我吗?先谢谢了
这是 preg 替换,您可以使用它来删除所需属性以外的属性。
$html = preg_replace("/(<img\s)[^>]*(src=\S+)[^>]*(\/?>)/i", "", $html);
$html = preg_replace("/(<figure\s)[^>]*[^>]*(\/?>)/i", "", $html);
$html = preg_replace("/(<figcaption\s)[^>]*[^>]*(\/?>)/i", "", $html);
这将为您提供输出
<figure ><img src="https://sampleonly.com/wp-content/uploads/2020/01/my-picture-300x169.jpg"><figcaption >This is my picture text.</figcaption><figure>
您可以使用下面的代码 注意:如果您包含 jquery 则仅使用脚本,否则使用下面的 jquery link
<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>