I have a block of text with embedded strings similar to "[ ]blk_foto_1[ ]" and I want to filter said block so that the result is a text where "[ ]blk_foto_1[ ]" is replaced by "image1.jpg", then I tried to do it with regular expressions but I don't achieve what I want, and the truth is that I think I'm going to die and I still won't fully understand how "regex" work. So far I have something like this:
$cadena = 'lorem vestibulum torquent at [***]blk_foto_1[***] orttitor habitasse mattis [***]blk_foto_2[***] platea taciti';
$patrones = [0=>'/[***]blk_foto_1[***]/', 1=>'/[***]blk_foto_2[***]/'];
$imagenes = [0 => 'imagen1.jpg', 1 => 'imagen2.jpg'];
echo preg_replace($patrones, $imagenes, $cadena);
But it returns me exactly the same original string with no changes.
My expectation is that the resulting string would replace '[ ]blk_photo_1[ ]' with 'image1.jpg' and so on, where the indices of $patterns match the indices of $images
You don't need to use preg_replace for that.
str_replace
accepts arrays as arguments:I leave you a working fiddle
PS: if what you want to replace is not literally like you showed us, and you really need to use a regular expression, you would have to escape the special characters (* [ and ] for starters).