简体   繁体   English

将html编码的字符串转换为数组

[英]Turn html encoded string into array

I have this api call that returns a string, contaning a chunk of images. 我有这个api调用,它返回一个字符串,包含一堆图像。 How can i convert that string into an array? 如何将字符串转换为数组?

The string that gets returned lookls like below. 返回的字符串如下所示。 Starts with a 开头为

tag since its a wordpress post. 标记,因为它是一个wordpress帖子。

Its all the image tags src is want in an array. 它的所有图像标签src都需要放在数组中。

<p><img src=\"http:\/\/local.pp.com\/api\/wp-content\/uploads\/2016\/10\/thumb-12.jpg\" alt=\"thumb\" width=\"720\" height=\"510\" class=\"alignnone size-full wp-image-52\" srcset=\"http:\/\/local.pp.com\/api\/wp-content\/uploads\/2016\/10\/thumb-12.jpg 720w, http:\/\/local.pp.com\/api\/wp-content\/uploads\/2016\/10\/thumb-12-300x213.jpg 300w\" sizes=\"(max-width: 709px) 85vw, (max-width: 909px) 67vw, (max-width: 984px) 61vw, (max-width: 1362px) 45vw, 600px\" \/><br \/>\n<img src=\"http:\/\/local.pp.com\/api\/wp-content\/uploads\/2016\/10\/thumb-12.jpg\" alt=\"thumb\" width=\"720\" height=\"510\" class=\"alignnone size-full wp-image-52\" srcset=\"http:\/\/local.pp.com\/api\/wp-content\/uploads\/2016\/10\/thumb-12.jpg 720w, http:\/\/local.pp.com\/api\/wp-content\/uploads\/2016\/10\/thumb-12-300x213.jpg 300w\" sizes=\"(max-width: 709px) 85vw, (max-width: 909px) 67vw, (max-width: 984px) 61vw, (max-width: 1362px) 45vw, 600px\" \/><br \/>\n<img src=\"http:\/\/local.pp.com\/api\/wp-content\/uploads\/2016\/10\/thumb-12.jpg\" alt=\"thumb\" width=\"720\" height=\"510\" class=\"alignnone size-full wp-image-52\" srcset=\"http:\/\/local.pp.com\/api\/wp-content\/uploads\/2016\/10\/thumb-12.jpg 720w, http:\/\/local.pp.com\/api\/wp-content\/uploads\/2016\/10\/thumb-12-300x213.jpg 300w\" sizes=\"(max-width: 709px) 85vw, (max-width: 909px) 67vw, (max-width: 984px) 61vw, (max-width: 1362px) 45vw, 600px\" \/><br \/>\n<img src=\"http:\/\/local.pp.com\/api\/wp-content\/uploads\/2016\/10\/thumb-12.jpg\" alt=\"thumb\" width=\"720\" height=\"510\" class=\"alignnone size-full wp-image-52\" srcset=\"http:\/\/local.pp.com\/api\/wp-content\/uploads\/2016\/10\/thumb-12.jpg 720w, http:\/\/local.pp.com\/api\/wp-content\/uploads\/2016\/10\/thumb-12-300x213.jpg 300w\" sizes=\"(max-width: 709px) 85vw, (max-width: 909px) 67vw, (max-width: 984px) 61vw, (max-width: 1362px) 45vw, 600px\" \/><br \/>\n<img src=\"http:\/\/local.pp.com\/api\/wp-content\/uploads\/2016\/10\/thumb-12.jpg\" alt=\"thumb\" width=\"720\" height=\"510\" class=\"alignnone size-full wp-image-52\" srcset=\"http:\/\/local.pp.com\/api\/wp-content\/uploads\/2016\/10\/thumb-12.jpg 720w, http:\/\/local.pp.com\/api\/wp-content\/uploads\/2016\/10\/thumb-12-300x213.jpg 300w\" sizes=\"(max-width: 709px) 85vw, (max-width: 909px) 67vw, (max-width: 984px) 61vw, (max-width: 1362px) 45vw, 600px\" \/><\/p>\n"
var div = document.createElement('div');
div.innerHTML = myHTMLString;
var images = div.getElementsByTagName('img');

Create a div, put the html in it and then use getElementsByTagName to get the images. 创建一个div,将html放入其中,然后使用getElementsByTagName获取图像。

getElementsByTagName returns an HTMLCollection, which is just like an array and can be iterated through the same way with an for loop. getElementsByTagName返回一个HTMLCollection,它就像一个数组,并且可以通过for循环以相同的方式进行迭代。

for(var i=images.length; i--;){
    var image = images[i];
    // do stuff with each image
}

Edit: you say you want the src's in the array. 编辑:您说您想要src在数组中。 look at this: https://jsfiddle.net/kev7hr2y/ 看这个: https : //jsfiddle.net/kev7hr2y/

You can use the DOMParser to parse the string into DOM elements, then find all <img> and map() their src attribute: 您可以使用DOMParser将字符串解析为DOM元素,然后找到所有<img>map()src属性:

var parser = new DOMParser(),
    parsedElms = parser.parseFromString(yourHTMLString, 'text/html'),
    imgs = parsedElms.getElementsByTagName('img');

var result = Array.prototype.map.call(imgs, function(e) { return e.getAttribute('src'); });

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM