Warning: file_get_contents(/data/phpspider/zhask/data//catemap/5/spring-mvc/2.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
在PHP中删除HTML atribute_Php_Html_Image_Figure - Fatal编程技术网

在PHP中删除HTML atribute

在PHP中删除HTML atribute,php,html,image,figure,Php,Html,Image,Figure,我有如下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-300

我有如下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>
<figure>
    <img src="https://sampleonly.com/wp-content/uploads/2020/01/my-picture.jpg"/>
    <figcaption>This is my picture text.</figcaption>
<figure>

这是我的图片文本。
我想删除id、样式、类等。最终代码如下:

<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>
<figure>
    <img src="https://sampleonly.com/wp-content/uploads/2020/01/my-picture.jpg"/>
    <figcaption>This is my picture text.</figcaption>
<figure>

这是我的图片文本。

有人能帮我吗?之前谢谢您。

这是preg replace,您可以使用它删除所需属性以外的属性

$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);
$html=preg\u replace(“/(]*(\\/?>)/i”、“$1$2$3”、$html);
$html=preg\u replace(“/(]*[^>]*(\\/?>)/i”、“$1$2$3”、$html);
$html=preg\u replace(“/(]*[^>]*(\\/?>)/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>
这是我的图片文本。
您可以使用以下代码 注意:如果您已经包含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>

$('figure,figcaption,img').removeAttr(“id样式类高度宽度大小srcset aria descripbedby”);

考虑使用DOM解析器,而不是更可靠的结果。我尝试DOM解析器,但它不工作,使用下面的代码“OOTUSKETONE”DOW投票的具体原因。因为我发现只有这样才能得到这项工作。