Get SRC attribute value of an image in a string II

Asked

Viewed 53 times

0

Dear Anderson, after studying your code in PHP and adapting it to my needs. A second question I’m working with objects and not as an array, if by chance there are two identical id for different images? How to convert to array to use a foreach, and it show me the two different images of an equal id. I hope I made myself clear in my question. Hugs,

  • Rogarfil, your question doesn’t make any sense the way it is. The question must be complete on its own, without depending on any other information, so please edit the question and make a full inquiry. Remember that the attribute id in HTML characterizes an element single in the document. If your application has more than one widget with the same id, then there must be something wrong.

  • I did not heed this fact as logical as the senseless question, I apologize and thank you for your attention in my elucidations...

  • I think you should at least put the link to the Anderson question. I myself have no idea what it is you’re talking about.

  • Related: https://answall.com/q/250357/132

  • You can even collect elements with the same id, but this escapes from the patterns. In case, it would have to pull by the tag, which makes it more complicated.

  • @DVD even made a test on my server with same id and worked, but repl.it only returned one src (the first one)

  • @Leocaracciolo I edited my comment above. I would have to ignore the id and pull by the tag name or anything else in common.

  • @Leocaracciolo That is, the id would be useless, therefore, it does not make sense to use same id.

  • @Leocaracciolo This is easy. Just pull by the tag name.

  • @Leocaracciolo Even more if the elements are inside a container, like: <div id="aba"><span id="1"></span><span id="1"></span></div>. Just take each span inside the div with id tab.

  • @Leocaracciolo Ixi, nothing on that link.

  • @DVD forgot to save https://repl.it/NYCi

  • @Leocaracciolo Normal. Vc looped by tagname and took only those who had the specified id.

  • @Leocaracciolo As I said, it works, but it escapes from the standards rs.

  • @Leocaracciolo If pull by the id, always the first is the valid, the others are ignored.

  • @DVD got it, so if Rogarfil wants to use the id, just change to if(strstr($image->getAttribute('id'),'img_blog')==true){ I went, good night!

Show 11 more comments

1 answer

2


Regarding the use of equal ids bear in mind that ids are a way to identify an element, and should be UNIQUE for each element, so use classes which are a way of identifying a group of elements.

$htmlImagens =<<<DEMO
  <img src="/images/blog/outra-imagem-A.jpg" />
  <img class="img_blog" src="/images/blog/outra-imagem-B.jpg" />
  <img class="img_blog" src="/images/blog/cliente-ideal-voce-sabe-quem-e.jpg" />
  <img src="/images/blog/outra-imagem-C.jpg" />
  <img src="/images/blog/outra-imagem-D.jpg" />
DEMO;

$dom = new DOMDocument();
$dom->loadHTML($htmlImagens);
//fazemos um loop procurando por ocorrências da Tag “img”
foreach($dom->getElementsByTagName('img') as $image) {
    //destacamos somente aquelas com classe "img_blog"
    if(strstr($image->getAttribute('class'),'img_blog')==true){
       // o array com os src das tags img cuja classe é "img_blog"
       $images[] = $image->getAttribute('src');
    }
}
print_r($images);

See working on repl it.

Bibliography

Domdocument

foreach

strstr

  • Now I get it. Pretty cool.

  • 1

    @DVD, that’s what I got, he wants an array of image src with certain, equal ids, only I changed to class

Browser other questions tagged

You are not signed in. Login or sign up in order to post.