Warning: file_get_contents(/data/phpspider/zhask/data//catemap/0/xml/14.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
使用PHP更新XML属性,但结果是;Can';t在写上下文中使用方法返回值“;_Php_Xml - Fatal编程技术网

使用PHP更新XML属性,但结果是;Can';t在写上下文中使用方法返回值“;

使用PHP更新XML属性,但结果是;Can';t在写上下文中使用方法返回值“;,php,xml,Php,Xml,我需要你的帮助。关于如何从给定的XML数据更改img src,有点困惑 返回了错误: Can't use method return value in write context 如何解决这个问题?任何帮助都将不胜感激。谢谢祝你今天愉快 <?php $question_data = '<p>My questions here....</p><p>&nbsp;</p><p><img class="img-respo

我需要你的帮助。关于如何从给定的XML数据更改img src,有点困惑

返回了错误:

Can't use method return value in write context
如何解决这个问题?任何帮助都将不胜感激。谢谢祝你今天愉快

<?php

$question_data = '<p>My questions here....</p><p>&nbsp;</p><p><img class="img-responsive" src="/uploads/images/questions/93_20161017102613.jpg" style="max-height:400px;" /></p>';

$xml = new DOMDocument();
$xml->loadHTML($question_data, LIBXML_HTML_NOIMPLIED | LIBXML_HTML_NODEFDTD);


echo '<pre>';

//Initial Dom

//Find only image and convert url

//Process to convert url
$imgNodes = $xml->getElementsByTagName('img');


echo '<br/>';
$arr_image_file_names = [];

for ($i = $imgNodes->length - 1; $i >= 0; $i--) {
    $imgNode = $imgNodes->item($i);
    $image_file_names = pathinfo($imgNode->getAttribute('src'), PATHINFO_BASENAME);

    if(!empty($image_file_names)):
    // Replace with new src

    $imgNode->getAttribute('src') = 'http://myurl/qst/def/img/v1/'.$image_file_names;

    endif;
}

echo '<br/>';
echo htmlentities($xml->saveHTML());

//Update into new array


//Convert back to DOM


echo '</pre>';


?>
loadHTML($question_data,LIBXML_HTML_noimpled,LIBXML_HTML_NODEFDTD);
回声';
//初始Dom
//仅查找图像并转换url
//转换url的进程
$imgNodes=$xml->getElementsByTagName('img');
回声“
”; $arr_image_file_name=[]; 对于($i=$imgNodes->length-1;$i>=0;$i--){ $imgNode=$imgNodes->item($i); $image\u file\u names=pathinfo($imgNode->getAttribute('src'),pathinfo\u BASENAME); 如果(!empty($image\u file\u names)): //替换为新的src $imgNode->getAttribute('src')='http://myurl/qst/def/img/v1/“.$image\u文件\u名称; endif; } 回声“
”; echo htmlentities($xml->saveHTML()); //更新到新阵列 //转换回DOM 回声'; ?>
您应该使用


getAttribute
函数的结果是一个字符串(而不是一个变量),您不能将新值分配给字符串(或者在本例中是函数的返回值)。

谢谢!你救了我的命!。你能告诉我如何添加属性吗?类似于:
$imgNode->addAtribute('style','max-width:100%”)?否,与
setAttribute
相同
setAttribute
如果属性不存在,则添加该属性。
$imgNode->setAttribute('src', 'http://myurl/qst/def/img/v1/'.$image_file_names);