PHP Illegal offset type

2019-01-19 10:40发布

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'>"

标签: php warnings
4条回答
smile是对你的礼貌
2楼-- · 2019-01-19 11:10

See first comment on this PHP bug report:

You cannot use arrays or objects as keys. Doing so will result in a warning: Illegal offset type. Check your code.

Ensure that $img_tag is of the appropriate variable type.

查看更多
Rolldiameter
3楼-- · 2019-01-19 11:13

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.

查看更多
Explosion°爆炸
4楼-- · 2019-01-19 11:20

$result is 2-dimentional array.So, $img_tag should be an array.

But only integers and strings may be used as offset

查看更多
Root(大扎)
5楼-- · 2019-01-19 11:26
foreach( $result[0] as $img_tag)

it works

查看更多
登录 后发表回答