Forum Moderators: coopster

Message Too Old, No Replies

Illegal string offset, what is this?

         

csdude55

1:58 pm on Nov 12, 2019 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member Top Contributors Of The Month



I'm finding a bunch of these in my error log:

PHP Warning: Illegal string offset '@attributes' in /home/foo/bar.php on line 169, referer: https://www.example.com/

PHP Warning: Illegal string offset 'href' in /home/foo/bar.php on line 169, referer: https://www.example.com/


bar.php is a new script I've recently built that turned out to be WAY more complicated than anticipated. I'm fetching third-party RSS feeds, tearing them apart in to usable chunks, then printing my formatted results.

Line 169 is actually blank, so I'm not entirely sure why that line is being referenced in the error. But here's where I think the problem lies:

if ($key['link']['@attributes']['href'] &&

// make sure it's not an array
!is_array($key['link']['@attributes']['href']) &&

// if it's less than 7 characters then it should be ignored
strlen($key['link']['@attributes']['href']) > 7) {
$link = str_replace('&', '&', $key['link']['@attributes']['href']);
parse_str($link, $ga_url);

if ($ga_url['url'])
$key['link'] = $ga_url['url'];

else
$key['link'] = $key['link']['@attributes']['href'];
}


After googling I think that the problem is that, sometimes, $key['link']['@attributes']['href'] is an array instead of a string. That's why I added the !is_array() command, but I'm still getting the error in my log.

Can you guys and gals suggest any other reason for the Illegal string offset warnings, or how to prevent them?

JorgeV

2:31 pm on Nov 12, 2019 (gmt 0)

WebmasterWorld Senior Member 5+ Year Member Top Contributors Of The Month



Hello-

It means that, sometimes, $key['link']['@attributes'] doesn't exist.

You can use this code, to test if it exists.
if(isset($key['link']['@attributes']))

csdude55

2:51 pm on Nov 14, 2019 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member Top Contributors Of The Month



I think you were right, @JorgeV ! So far, no more errors...