Delete images sent by ckeditor

Asked

Viewed 90 times

1

I am using the Ckeditor editor. When I put an image in the text in the database it is stored as follows:

<p><img alt="" src="http://intranet.supersoft.com.br/novo/ckeditor-integrated/uploads/images/imagem.jpg" style="height:420px; width:660px" /></p>

<p>&nbsp;</p>

<p>Muita gente ficou incomodada ao saber que o WhatsApp havia adicionado ....

How to take just the image path for me to use the unlink when I remove the post the image is also removed from the folder?

  • Furlan, your image will always be inside tags?

  • yes @gmsantos, the code in the question, is so on the bank line

1 answer

1


You can use the DOM to manipulate your HTML in PHP.

<?php

$string = '<p><img alt="" src="http://intranet.supersoft.com.br/novo/ckeditor-integrated/uploads/images/imagem.jpg" style="height:420px; width:660px" /></p>

<p>&nbsp;</p>

<p>Muita gente ficou incomodada ao saber que o WhatsApp havia adicionado ....';

$dom = new DOMDocument();
$dom->loadHTML($string);
$imagesTag = $dom->getElementsByTagName('img');

foreach ($imagesTag as $img){
    echo $img->getAttribute('src');
}

This code will create a DOMElement for each tag <img> of your code, and write the content of the attribute src.

From there, just apply your logic to do the unlink of the archive.

  • 1

    Dude, thanks, I’ll study more on DOM, thanks!

Browser other questions tagged

You are not signed in. Login or sign up in order to post.