我有以下字符串
<img src="data:image/png;base64,iVBORw0KGgoAAAANSUhEUgAABVYAAAMACAIAAABAXKuVAAAAA3NCSVQICAjb4U/gAAAgAElEQVR4Xu....">
现在我想将“uploads / imag1.jpeg”替换为
"data:image/png;base64,iVBORw0KGgoAAAANSUhEUgAABVYAAAMACAIAAABAXKuVAAAAA3NCSVQICAjb4U/gAAAgAElEQVR4Xu...."
我试试这个
$doc = new DOMDocument();
$doc->loadHTML('"data:image/png;base64,iVBORw0KGgoAAAANSUhEUgAABVYAAAMACAIAAABAXKuVAAAAA3NCSVQICAjb4U/gAAAgAElEQVR4Xu...."');
$tags = $doc->getElementsByTagName('img');
foreach ($tags as $t)
{
$t->setAttribute('src','uploads/imag1.jpeg');
echo $doc->saveHTML();
}
但这不能正常运作。 我有正确路径的路径
localhost:83/mysite/images/imag1.jpg
但是我的代码给了我错误的路径,
localhost:83/mysite/image/uploads/imag1.jpg
由于此问题,更换无效。
我该如何替换呢?
答案 0 :(得分:0)
这是PHP的解决方案:
<?php
echo '<img src="data:image/png;base64,'. base64_encode(file_get_contents("uploads/imag1.jpeg")) .'"/>;
?>