Matoeil
Matoeil

Reputation: 7289

php preg_replace : strip all <img> elements except for a certain img id

Here is the function i am using to remove every img element from my wordpress string content:

function strip_images($content){ 
                        return preg_replace('/<img[^>]+./','',$content);
                    }

How could i exclude from stripping the img of a certain id?

Upvotes: 0

Views: 264

Answers (1)

Jakub Matczak
Jakub Matczak

Reputation: 15656

Try

/<img[^>]+id="someId[^>]+./

Tested on

<img id="someId">
    something else
    <img class="someClass" id="someId">
    <img class="someClass" id="someOtherId">
    <img class="someClass" id="someId" alt="Hey">

Chooses 1st and 3rd line.

If you do much more such operations on HTML in PHP you can use for example phpQuery for this job.

Upvotes: 1

Related Questions