How to deal with incompatible XPath expression expression in PHP

I wrote the following line to analyze images from twitter profiles, and it works most of the time, except that this line kills it containing the script when the page does not contain a match.

I am more familiar with the regular expression syntax, but thought I would try something new and learn the xpath process, but I'm not sure what I am missing? I tried the try / catch logic and this did not help. How can I prevent this xpath request from stopping the script and just returning '' if no match is found?

$img = '';
$img = @$xpath->query("//*[@class='profile-img' or @id='profile-image']" )->item( 0 )->getAttribute( 'src' );

thank

+3
source share
1 answer

@, . query, item(0) NULL. getAttribute NULL .

:

$img = $xpath->query("//*[@class='profile-img' or @id='profile-image']")->item(0);
$img = $img ? $img->getAttribute('src') : '';

: $img , src. .

+1

Source: https://habr.com/ru/post/1790013/


All Articles