将代码从javascript重写为PHP
我有javascript代码:将代码从javascript重写为PHP,javascript,php,xml,Javascript,Php,Xml,我有javascript代码: if (imageData[imageCount].comments.data != null) { text = 'Comments Data:<br />'; imageData[imageCount].comments.data.forEach(function(comment){ text += comment.from.username + ': ' + comment.text + '<br />'; }); } fo
if (imageData[imageCount].comments.data != null)
{
text = 'Comments Data:<br />';
imageData[imageCount].comments.data.forEach(function(comment){
text += comment.from.username + ': ' + comment.text + '<br />';
});
}
foreach($contents->data as$data){
foreach($data->comments->data as$comment){
if(preg#u match(“/#title/”,$comment->text)){
$komen=preg#u replace(“/#title/”,“$comment->text);
打破
}else{$komen='No Title';}
}
foreach($data->comments->data as$comment){
if(preg#u match(“/#price/”,$comment->text)){
$harga=preg#u replace(“/#price/”,“$comment->text);
打破
}else{$harga='0';}
}
回声'
低分辨率->url。“”width=“100%”/>
“.$data->caption->text”
]]>
您在最后一行忘记了$,一些括号设置不正确。要在PHP中使用concat字符串,请使用“.”而不是“+”
foreach($contents->data as$data){
如果($data->comments->data!=null){
foreach($data->comments->data as$comment){
$text=$comment->from->username.':'.$comment->text.
;
}
}
}
comment
会引起问题。$comment
,也许?当然,在PHP中,
是concatation操作符。你在做一堆“字符串数学”“使用+
.PHP也不使用+
进行连接。$contents->data
和$data->comments->data
数组吗?您没有指出是哪个foreach导致了问题。//初始化类$instagram=new instagram($accessToken);$instagram->setAccessToken($accessToken);$contents=$instagram->getUserMedia($userID,$settings['count']);我已经编辑了这个问题。请查看上面的内容,确保包含的数据是对象、数组还是字符串:)
foreach ($contents->data as $data) {
if ($data->comments->data != null)
{
foreach($data->comments->data as $comment)
{
$text = comment->from->username + ': ' + comment->text + '<br />';
});
}
<products>
<category categoryName="Instagram">
<?php
foreach($data->comments->data as $comment){
if(preg_match('/#title/', $comment->text)){
$komen = preg_replace('/#title/', '', $comment->text);
break;
} else { $komen = 'No Title'; }
}
foreach($data->comments->data as $comment){
if(preg_match('/#price/', $comment->text)){
$harga = preg_replace('/#price/', '', $comment->text);
break;
} else { $harga = '0'; }
}
echo '<product productName="'. $komen .'" productID="' . $data->id . '" thumbPath="' . $data->images->thumbnail->url . '" productPrice="'. $harga .'">
<details>
<![CDATA[
<img src="' . $data->images->low_resolution->url . '" width="100%"/>
' . $data->caption->text . '
]]>
</details>
</product>
foreach ($contents->data as $data) {
if ($data->comments->data != null) {
foreach ($data->comments->data as $comment) {
$text = $comment->from->username . ': ' . $comment->text . '<br />';
}
}
}