1

I have a HTML code like this:

<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>

i want to remove id, style, class etc. The final code like this:

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

is anybody here can help me? thank you before.

Calos
  • 1,783
  • 19
  • 28
oyotsuket
  • 39
  • 6

2 Answers2

1

Here is the preg replace that you can use to remove the attribute other than the required one.

$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);

this will give you the output

<figure ><img src="https://sampleonly.com/wp-content/uploads/2020/01/my-picture-300x169.jpg"><figcaption >This is my picture text.</figcaption><figure>
Amit Sharma
  • 1,775
  • 3
  • 11
  • 20
1

You can use below code Note: If you have include jquery then use only script otherwise use below 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>
Jayshri Ghoniya
  • 266
  • 1
  • 9