我有这个代码用它们各自的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-
现在我该如何替换&#34;中包含的图像src? - &#34;到上面的完整图片标签?
答案 0 :(得分:0)
您可以将值存储在数组中。现在你可以使用这个数组来做进一步的工作。
$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);