How to replace each match instance of a regular expression with a different value?

4

I have text (html code) with several images in standard HTML format:

<img src="X" atributos />

I need the src attribute value to be replaced with the CID: # identifier where # is a unique value that identifies each image (see in the code below). That is, each image will have a different value within that attribute.

Below the code I've done so far, it already matches the images correctly. But how to do this replacement?

Another important information is that I need to have in the code the information of which unique identifier has replaced which value (image url) of the src attribute. For example, I need to have a way of knowing that identifier 354 replaced the value "img / xxx.jpg".

preg_match_all('/<img src=[",\']([^>,^\',^"]*)[",\']([^>]*)/', $html, $matches);

$url_imagem = array();
$atributos_imagem = array();
$cid = array();
$contador = 0;

foreach ($matches[1] as $i => $img){

    $url_imagem[$contador] = $matches[2][$i];
    $atributos_imagem[$contador] = $matches[3][$i];

    //Como substituir o conteudo do atributo SRC com o valor da variável $cid abaixo?
    $cid[$contador] = "CID:".date('YmdHms').'.'.time();

    $contador++;   
}
    
asked by anonymous 27.06.2018 / 18:53

1 answer

4

First this $contador = 0; is unnecessary in foreach , since the variable $i already returns the indexes starting from 0 , that is, where you use $contador can replace with $i .

Another problem is the value you are getting using date and hours :

$cid[$i] = "CID:".date('YmdHms').'.'.time();

In this way all values of $cid[$i] will have the same value because the loop will run in a way that does not differentiate system time. This means that with each loop in the loop, the time will be the same and if it is to replace the% s of the images, all will have the same value.

To make sure that each value will be different, you can add another unique value, the src itself:

                                     concatenar o $i
                                            ↓
$cid[$i] = "CID:".date('YmdHms').'.'.time().$i;

And finally, to make the substitution, you can use $i replacing the first occurrence:

$html = preg_replace('/'.$img.'/', $cid[$i], $html, 1);

I also created a preg_replace array to store% s of% s replaced so you can know what they were and in what sequence.

Example:

Getting Started:

$html = '<img src="link1" atributos />
<img src="link2" atributos />
<img src="link1" atributos />';

Note that the 3rd image has the same $identificador = array(); of the 1st. After the replacement, it looks something like this:

                                                ↓
$html = '<img src="CID:20180627170651.15301318110" atributos />
<img src="CID:20180627170651.15301318111" atributos />
<img src="CID:20180627170651.15301318112" atributos />';
                                       ↑

See in the arrows that src makes a value different from the other.

The code looks like this:

preg_match_all('/<img src=[",\']([^>,^\',^"]*)[",\']([^>]*)/', $html, $matches);

$url_imagem = array();
$atributos_imagem = array();
$cid = array();
$identificador = array();

foreach ($matches[1] as $i => $img){

    $url_imagem[$i] = $matches[1][$i];
    $atributos_imagem[$i] = $matches[2][$i];

    //Como substituir o conteudo do atributo SRC com a variável $cid abaixo?
    $cid[$i] = "CID:".date('YmdHms').'.'.time().$i;
    $tag_img = str_replace("/", "\/", $img);    
    $html = preg_replace('/'.$tag_img.'/', $cid[$i], $html, 1);
    $identificador[$i] = $img;

}
    
27.06.2018 / 22:48