replace img src value



我有这种格式的数据:

<img src="cid:ii_jhzwfy6l0_163c993284ad67a3" width="295" height="65">
<img src="cid:ii_jhzwfy6l0_163c99368434384384a3" width="295" height="65">
this is text with some images

这只是一个字符串。

我想用存储在数组中的实际值替换上述字符串的 src 值,如下所示:

[attachments] => Array
(
[0] => logo.png
[1] => sample.png
)

我该怎么做?

使用 regx 匹配 src - src\s*=\s*"(.+?("从 img 选项卡。 将值数组替换为数组 [0] 和 [1] ---- 希望这个帮助

$string = '<img src="cid:ii_jhzwfy6l0_xxx63c99676ghf4ad67a3" width="295" height="65">
<img src="cid:ii_jhzwfy6l0_163c993284ad67a3" width="295" height="65"><img src="cid:ii_jhzwfy6l0_163c99368434384384a3" width="295" height="65">';
$arr = [
'logo.png', 
'sample.png', 
'one.png'
];
echo preg_replace_callback('/cid:ii_[^"]+/', function($matches) use (&$arr) {
return array_shift($arr);
}, $string);

最新更新