Skip to content

Instantly share code, notes, and snippets.

@brcontainer
Last active April 12, 2017 01:45
Show Gist options
  • Save brcontainer/84c4c1fa213d2508590dd6eb70ac4dfb to your computer and use it in GitHub Desktop.
Save brcontainer/84c4c1fa213d2508590dd6eb70ac4dfb to your computer and use it in GitHub Desktop.
Get images in CDATA (FEED/RSS)
<?php
/*
* http://pt.stackoverflow.com/a/197684/3635
*/
$imagem = array();
$items = simplexml_load_file("http://cidades.gov.br/ultimas-noticias?format=feed&type=rss")->channel->item;
$doc = new DOMDocument;
foreach($items as $item) {
//Converte o objeto para string
$desc = (string) $item->description;
//Faz o parse da string html para DOMElement
if ($doc->loadHTML($desc)) {
//Pega todas tags img
$imgs = $doc->getElementsByTagName('img');
//Verifica se existe ao menos uma imagem
if ($imgs->length) {
//Salva a imagem no array
$imagem[] = $imgs->item(0)->getAttribute('src');
}
}
}
var_dump($imagem);
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment