preg_replace to change image src

0

I have several news items registered in a database, many of them have images that point directly to my site.

Only they have http, and since I've installed SSL on my site, I need them to have https.

I do not want to change anything in the database, so I need a preg_replace that changes from http to https, only tags in the src attribute.

This is not valid for links, just for images ...

Can anyone help me?

    
asked by anonymous 24.01.2017 / 16:53

3 answers

0
<?php

$re = '/([^<img src=\'"]+):/';

$str = '<img src="http://www.google.com.br"> <img src=\'http://www.google.com.br\'>';

preg_match_all($re, $str, $matches);

$str = preg_replace($re, 'https', $str);

print_r($str);
    
24.01.2017 / 17:14
0

You can make a simple replace replacing http://www.domain.com.br with your url.

$str = 'Lorem ipsum dolor sit amet, consectetur adipiscing elit.
        <img src="http://www.domain.com.br/imagem.jpeg"><imgsrc="http://www.domain.com.br/imagem.jpeg">';


echo str_replace( 'http://www.domain.com.br' , 'https://www.domain.com.br' , $str );
    
24.01.2017 / 17:32
0

example - ideone

$str = 'Bla bla bla
<img src="http://www.seudominio.com/image1.jpeg"><imgsrc="http://www.seudominio.com/image2.jpeg">
<a href="http://www.seudominio.com/">http://www.seudominio.com/</a>';

echo str_replace( '<img src="http','<imgsrc="https' , $str );
    
24.01.2017 / 23:30