2016-12-04 6 views
-1

私はあなたの助けが必要です。与えられたXMLデータからimg srcをどのように変更するのかちょっと分かりません。PHPを使用してXML属性を更新しましたが、結果は "書き込みコンテキストでメソッドの戻り値を使用できません"

エラーが返されました:

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


?> 

答えて

1

あなたはgetAttribute関数の結果が文字列(とない変数)である代わりにsetAttribute function

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

を使用する必要があり、あなたは(文字列に新しい値を割り当てることはできませんかこの場合は関数の戻り値)。

+0

ありがとうございます!あなたは私の日を救う! – Nere

+0

addAttributeの方法を教えてもらえますか?同じようですか: '$ imgNode-> addAtribute( 'style'、 'max-width:100%');'? – Nere

+1

いいえ、 'setAttribute'と同じです。' setAttribute'は属性が存在しなければそれを追加します。 – Dekel

関連する問題