从文本中提取图像src? [英] extract image src from text?
本文介绍了从文本中提取图像src?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我有一个变量$ content,其中包含一些这种形式的文本和图像(图像数量未知):
I have a variable $content that contains some text and images in this form (unknown amount of images):
text text text text <img src="path/to/image/1">text text text text
<img src="path/to/image/2">
text text text text text text text text text text text text text text text text <img src="path/to/image/3"><img src="path/to/image/4">text text text text
<img src="path/to/image/5">
我想提取所有图像src并使用php将它们存储在数组中,如下所示:
I want to extract all images src and store them in array using php like so:
array(
[1]="path/to/image/1"
[2]="path/to/image/2"
[3]="path/to/image/3"
[4]="path/to/image/4"
[5]="path/to/image/5"
.
.
.
)
做这样的事情的最好方法是什么.我已经尝试过函数爆炸,但是这种方式似乎效率很低.
what is the best way to do something like this. I already tried the function explode but this way seemed inefficient.
推荐答案
$dom = new domDocument;
$dom->loadHTML($html);
$dom->preserveWhiteSpace = false;
$imgs = $dom->getElementsByTagName("img");
$links = array();
for($i = 0; $i < $imgs->length; $i++) {
$links[] = $imgs->item($i)->getAttribute("src");
}
这篇关于从文本中提取图像src?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文