M
M
midarovrk2021-06-14 00:31:25
PHP
midarovrk, 2021-06-14 00:31:25

How to check if an img tag has an attribute in PHP?

Tell me what I'm doing wrong?

I find the necessary img on the page in the right container, and then I need to check if they have the data-src attribute.

I do this:

foreach($data->find('.chapter-container .chapter-images img') as $img){

if(property_exists(is_array($img->getAttribute('data-src')))) 
  {
    echo "[ есть ]";
  } else {
    echo "[ нет ]";
}

Also tried this:

foreach($data->find('.chapter-container .chapter-images img') as $img){

 if(property_exists($img, 'data-src'))
  {
    echo "[ есть ]";
  } else {
    echo "[ нет ]";
}

Both options don't work, what's wrong?

Answer the question

In order to leave comments, you need to log in

1 answer(s)
O
Oleg Frolov, 2021-06-14
@midarovrk

If the parser has an is method, then this is possible.
By the way, what parser are you using?

foreach($data->find('.chapter-container .chapter-images img') as $img){
 if($img->is('[data-src]'))  {
    echo "[ есть ]";
  } else {
    echo "[ нет ]";
 }
}

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question