arrays - PHP - Replace string src value with data uri -
good day.
i have array holds data uri of img.
$var = array( [0] => data:image/png;base64,ivborw0kggoaa... [1] => data:image/png;base64,rw0kkggoaswaa... );
and have string.
$foo = '<p>test 7/31/2</p> <p> <img src="/media/1.png" alt="" /></p> <p><img src="/media/2.png" alt="" /></p>';
how can replace src
in $foo
data uri array in $var
?
result like.
$foo = '<p>test 7/31/2</p> <p> <img src="data:image/png;base64,ivborw0kggoaa..." alt="" /></p> <p><img src="data:image/png;base64,rw0kkggoaswaa..." alt="" /></p>';
any ideas do!
try this:
$images = [ "/media/1.png" => 'data:image/png;base64,ivborw0kggoaa...', "/media/2.png" => 'data:image/png;base64,rw0kkggoaswaa...' ]; $url = '<p>test 7/31/2</p> <p> <img src="/media/1.png" alt="" /></p> <p><img src="/media/2.png" alt="" /></p>'; $url = str_replace(array_keys($images), array_values($images), $url);
Comments
Post a Comment