Delete images sent by ckeditor

1

I'm 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 do I get only the path of the image so that I use unlink when I remove the post the image is also removed from the folder?

    
asked by anonymous 17.11.2014 / 11:15

1 answer

1

You can use 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 contents of the src attribute.

From there, just apply your logic to make unlink of the file.

    
17.11.2014 / 12:00