gpt4 book ai didi

php - 用字符串中的完整图像标签替换图像 src

转载 作者:行者123 更新时间:2023-11-28 02:34:06 26 4
gpt4 key购买 nike

我有这段代码可以用它们各自的 src.. 替换字符串中的图像标签。

$url='<img class="emojioneemoji" src="http://localhost/sng/assets/js/plugins/em/2.1.4/assets/png/1f602.png">checking<img class="emojioneemoji" src="http://localhost/sng/assets/js/plugins/em/2.1.4/assets/png/1f601.png"><img class="emojioneemoji" src="http://localhost/sng/assets/js/plugins/em/2.1.4/assets/png/1f62c.png">working check<img class="emojioneemoji" src="http://localhost/sng/assets/js/plugins/em/2.1.4/assets/png/1f600.png">';


$doc = new DOMDocument();
@$doc->loadHTML($url);

$tags = $doc->getElementsByTagName('img');
$str = "-" ;
foreach ($tags as $tag) {
$img_path = $tag->getAttribute('src');
$directory = $img_path;
$ee = pathinfo($directory);
$pic_name= $ee['basename'];
$next = "" ;
if ($tag->nextSibling && get_class($tag->nextSibling) == "DOMText") {
$next = $tag->nextSibling->wholeText . "-" ;
}
$str .= $pic_name . "-" . $next ;
}
echo $str ;

上面代码的输出是

-1f602.png-checking-1f601.png-1f62c.png-working check-1f600.png-

现在如何将这个包含在“-”中的图像 src 替换为上面的完整图像标签?

最佳答案

您可以将值存储在数组中。现在您可以使用这个数组做进一步的工作。

$url='<img class="emojioneemoji" src="http://localhost/sng/assets/js/plugins/em/2.1.4/assets/png/1f602.png">checking<img class="emojioneemoji" src="http://localhost/sng/assets/js/plugins/em/2.1.4/assets/png/1f601.png"><img class="emojioneemoji" src="http://localhost/sng/assets/js/plugins/em/2.1.4/assets/png/1f62c.png">working check<img class="emojioneemoji" src="http://localhost/sng/assets/js/plugins/em/2.1.4/assets/png/1f600.png">';


$doc = new DOMDocument();
@$doc->loadHTML($url);

$tags = $doc->getElementsByTagName('img');
$str = [] ;
$i = 0;

foreach ($tags as $tag) {
$img_path = $tag->getAttribute('src');
$directory = $img_path;
$ee = pathinfo($directory);
$pic_name= $ee['basename'];
$next = "" ;
if ($tag->nextSibling && get_class($tag->nextSibling) == "DOMText") {
$next = $tag->nextSibling->wholeText;
}

array_push($str, ['src' => $pic_name, 'next' => $next]);
}

echo "<pre>";
print_r($str);

关于php - 用字符串中的完整图像标签替换图像 src,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/48521739/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com