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
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 sameid
, then there must be something wrong.– Woss
I did not heed this fact as logical as the senseless question, I apologize and thank you for your attention in my elucidations...
– Rogarfil
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.
– Victor Stafusa
Related: https://answall.com/q/250357/132
– Victor Stafusa
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.– Sam
@DVD even made a test on my server with same id and worked, but repl.it only returned one src (the first one)
– user60252
@Leocaracciolo I edited my comment above. I would have to ignore the id and pull by the tag name or anything else in common.
– Sam
@Leocaracciolo That is, the id would be useless, therefore, it does not make sense to use same id.
– Sam
@Leocaracciolo This is easy. Just pull by the tag name.
– Sam
@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.– Sam
@Leocaracciolo Ixi, nothing on that link.
– Sam
@DVD forgot to save https://repl.it/NYCi
– user60252
@Leocaracciolo Normal. Vc looped by tagname and took only those who had the specified id.
– Sam
@Leocaracciolo As I said, it works, but it escapes from the standards rs.
– Sam
@Leocaracciolo If pull by the id, always the first is the valid, the others are ignored.
– Sam
@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!– user60252