Douglas Roos
Douglas Roos

Reputation: 613

Need to replace part of img src with PHP

I have an HTML document, i need to replace the img src with the one i need, for example:

I have this inside a HTML document

<img src="img/lists/temp/1/12345.jpg">

I need to replace it for

<img src="img/lists/1/12345.jpg">

This is the code i use to call the HTML:

$link->query("INSERT INTO table(titulo,url,id_cate,id_quien,id_marca,filtros,descripcion,cantidad,precio,subasta,condicion,garantia,tipo_venta,envio,fstart,fend,plan,status,envio_precio) SELECT titulo,url,id_cate,id_quien,id_marca,filtros,descripcion,cantidad,precio,subasta,condicion,garantia,tipo_venta,envio,fstart,fend,plan,status,envio_precio FROM temp_table WHERE temp_table.id_publicacion = '$id'")){
                    $newId = $link->insert_id;
                    $description = str_replace("img/lists/temp/","img/lists/",$description);
                    $link->query("UPDATE table SET descripcion = '$description' WHERE id = '$newId'");

I've tried with:

$description = str_replace("img/lists/temp/","img/lists/",$description);

But it doesn't work and it breaks the content of the HTML

How can i achieve that? Also i can have more than one img in the HTML document.

Appreciate any help

Upvotes: 0

Views: 1462

Answers (2)

Robert
Robert

Reputation: 20286

Try something like this one:

$str = '<img src="img/lists/temp/1/12345.jpg">';
$rep = 'img/lists/1/12345.jpg';
$description = preg_replace('#(<img.*src=")[^"]+(".*>)#', "$1{$rep}$2", $str);

Upvotes: 1

Waxi
Waxi

Reputation: 1651

Do you have this...

$description = 'img/lists/temp/'

Before you do this...

$description = str_replace("img/lists/temp/","img/lists/",$description);

Upvotes: 1

Related Questions