Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

PHP Illegal offset type

Tags:

php

warnings

Warning: Illegal offset type in /email_HANDLER.php on line 85

$final_message = str_replace($from, $to, $final_message);

preg_match_all('/<img[^>]+>/i',$final_message, $result);
$img = array();
foreach($result as $img_tag)
{
    preg_match_all("/(alt|title|src)=('[^']*')/i",(string)$img_tag, $img[$img_tag]); //LINE 85
}

Anyone? I'm about to tear my hair out over this...

here is my var_dump of $img_tag

array(1) {
  [0]=>
  string(97) "<img alt='' src='http://pete1.netsos.com/site/files/newsletter/banner.jpg' align='' border='0px'>"
like image 980
VACIndustries Avatar asked Oct 11 '11 20:10

VACIndustries


1 Answers

Assuming $img_tag is an object of some type, rather than a proper string, cast $img_tag to a string inside the []

preg_match_all("/(alt|title|src)=('[^']*')/i",(string)$img_tag, $img[(string)$img_tag]);
//------------------------------------------------------------------^^^^^^^^^

Some object types, notably SimpleXMLElement for example, will return a string representation to print/echo via the magic method __toString(), but cannot stand in as regular strings. Attempts to use them as array keys will yield the illegal offset type error unless you cast them to proper strings via (string)$obj.

like image 135
Michael Berkowski Avatar answered Sep 21 '22 17:09

Michael Berkowski