[英]Embed HTML in PHP Code
I have to fetch data using a REST Api, and my response is in json. 我必须使用REST Api来获取数据,而我的回复是json。 After decoding the response is in an array I have to display it in a webpage such that each of the responses in the array get displayed in a specific display format (Just Like tweets in twitter are displayed in a specific box type format).How to loop through the array in php and display it ? 解码后的响应位于数组中,我必须将其显示在网页中,以便数组中的每个响应都以特定的显示格式显示(就像twitter中的tweet都以特定的框类型格式显示一样)。在数组中循环遍历并显示它? The only solution I find is embedding the HTML in php using echo which I did but it is not working .And the debugger isn't helping much. 我发现的唯一解决方案是使用echo来将HTML嵌入php中,但我没有用。调试器的作用不大。 What am I missing ? 我想念什么?
Please advise what to do. 请告知该怎么办。
Here is my code: 这是我的代码:
echo "<div class="tweet original-tweet js-stream-tweet js-actionable-tweet js-profile- popup-actionable js-original-tweet my-tweet" data-tweet-id="515380319977893890"data-item-id="515380319977893890" data-name="" data-user-id="2665607941" data-expanded-footer="<div class="js-tweet-details-fixer tweet-details-fixer"> <div class="entities-media-container js-media-container" style="min-height:0px"> </div> <div class="js-machine-translated-tweet-container"></div> <div class="js-tweet-stats-container tweet-stats-container "> </div> <div class="client-and-actions"> <span class="metadata"> <span>11:00 PM - 25 Sep 2014</span> &middot; <a class="permalink-link js-permalink js-nav" href="/status/515380319977893890" tabindex="-1">Details</a> </span> </div> </div> " data-you-follow="false"data-you-block="false">" ;
echo "<div class="content">" ;
echo "<div class="stream-item-header">" ;
echo "<a class="account-group js-account-group js-action-profile js-user-profile-link js-nav" data-user-id="2665607941">
<img class="avatar js-action-profile-avatar" src="https://pbs.twimg.com/profile_images/506790785572814848/vdj_UAuZ_bigger.jpeg" alt="">
<strong class="fullname js-action-profile-name show-popup-with-id" data-aria-label-part> </strong>
<span>‏</span><span class="username js-action-profile-name" data-aria-label-part><s></s><b> </b></span>
</a>" ;
echo "<small class="time">
<a class="tweet-timestamp js-permalink js-nav js-tooltip" title="11:00 PM - 25 Sep 2014" ><span class="_timestamp js-short-timestamp "
data-aria-label-part="last" data-time="1411711218" data-time-ms="1411711218000" data-long-form="true">Sep 25</span></a>
</small>" ;
echo"</div>";
echo "<p class="js-tweet-text tweet-text" lang="en" data-aria-label-part="0"> </p>" ;
echo "<div class="stream-item-footer">";
echo "<ul class="tweet-actions js-actions">";
echo "<li class="more-tweet-actions">";
echo "<div class="action-more-container">";
echo "<div class="dropdown">";
echo "<button type="button" class="btn-link with-icn dropdown-toggle js-dropdown-toggle js-tooltip">";
echo "<span class="Icon Icon--delete"></span>";
echo "<b>Delete</b>";
echo "</button>";
echo "<div class="dropdown-menu">";
echo "<div class="dropdown-caret">";
echo "<div class="caret-outer"></div>";
echo "<div class="caret-inner"></div>";
echo "</div>";
echo "</div>";
echo "</div>";
echo "</div>";
echo "</li>";
echo "</ul>";
echo "</div>";
echo "</div>";
echo "</div>";
After Scott's answer I changed my code , but still it is not working and I am not getting it. 在Scott回答之后,我更改了代码,但仍然无法正常工作,也没有得到。
echo "<div class=\"tweet original-tweet js-stream-tweet js-actionable-tweet js-profile-popup-actionable js-original-tweet
my-tweet\\" data-tweet-id="515380319977893890\\"data-item-id=\\"515380319977893890\\" data-name=\\"\\" data-user-id=\\"2665607941\\" data-expanded-footer=\\"<div class="js-tweet-details-fixer tweet-details-fixer"> <div class="entities-media-container js-media-container" style="min-height:0px"> </div> <div class="js-machine-translated-tweet-container"></div> <div class="js-tweet-stats-container tweet-stats-container "> </div> <div class="client-and-actions"> <span class="metadata"> <span>11:00 PM - 25 Sep 2014</span> · my-tweet \\“ data-tweet-id =” 515380319977893890 \\“ data-item-id = \\” 515380319977893890 \\“ data-name = \\” \\“ data-user-id = \\” 2665607941 \\“ data-expanded-footer = \\“ <div class =” js-tweet-details-fixer tweet-details-fixer“> <div class =” entities-media-container js-media-container“ style =” min-height:0px“> </ div> <div class =“ js-machine-translated-tweet-container”> </ div> <div class =“ js-tweet-stats-container tweet-stats-container”> </ div> <div class =“ client-and-actions“> <span class =” metadata“> <span>下午11:00-2014年9月25日</ span>&middot; <a class="permalink-link js-permalink js-nav" href="/status/515380319977893890" tabindex="-1">Details</a> </span> </div> </div> " data-you-follow=\\"false\\"data-you-block=\\"false\\">" ; <a class="permalink-link js-permalink js-nav" href="/status/515380319977893890" tabindex="-1">详细信息</a> </ span> </ div> </ div> </ data> you-follow = \\“ false \\” data-you-block = \\“ false \\”>“;
echo "" ; 回声“”;
echo "<div class=\"stream-item-header\">" ;
echo "<a class=\"account-group js-account-group js-action-profile js-user-profile-link js-nav\" data-user-id=\"2665607941\
">
<img class=\"avatar js-action-profile-avatar\" src=\"https://pbs.twimg.com/profile_images/506790785572814848/vdj_UAuZ_bigger.jpeg\" alt=\"\">
<strong class=\"fullname js-action-profile-name show-popup-with-id\" data-aria-label-part> </strong>
<span>‏</span><span class=\"username js-action-profile-name\" data-aria-label-part><s></s><b> </b></span>
</a>" ;
echo "<small class=\"time\">
<a class=\"tweet-timestamp js-permalink js-nav js-tooltip\" title=\"11:00 PM - 25 Sep 2014\" ><span class=\"_timestamp js-short-timestamp \"
data-aria-label-part=\"last\" data-time=\"1411711218\" data-time-ms=\"1411711218000\" data-long-form=\"true\">Sep 25</span></a>
</small>" ;
echo"</div>";
echo "<p class=\"js-tweet-text tweet-text\" lang=\"en\" data-aria-label-part=\"0\"> </p>" ;
echo "<div class=\"stream-item-footer\">";
echo "<ul class=\"tweet-actions js-actions">";
echo "<li class=\"more-tweet-actions\">";
echo "<div class=\"action-more-container\">";
echo "<div class=\"dropdown\">";
echo "<button type=\"button" class=\"btn-link with-icn dropdown-toggle js-dropdown-toggle js-tooltip\">";
echo "<span class=\"Icon Icon--delete\"></span>";
echo "<b>Delete</b>";
echo "</button>";
echo "<div class=\"dropdown-menu\">";
echo "<div class=\"dropdown-caret\">";
echo "<div class=\"caret-outer\"></div>";
echo "<div class=\"caret-inner\"></div>";
echo "</div>";
echo "</div>";
echo "</div>";
echo "</div>";
echo "</li>";
echo "</ul>";
echo "</div>";
echo "</div>";
echo "</div>";
Check your single and double quotes. 检查您的单引号和双引号。 The strings are being returned are being truncated. 返回的字符串被截断。 This is invalid: 这是无效的:
echo "<div class="stream-item-header">" ;
Change it to: 更改为:
echo "<div class=\"stream-item-header\">" ;
Or: 要么:
echo "<div class='stream-item-header'>" ;
And repeat this fix for each of your echo lines. 并对每条回声线重复此修复程序。
For example, this: 例如,这:
echo "<div class="tweet original-tweet js-stream-tweet js-actionable-tweet js-profile- popup-actionable js-original-tweet my-tweet" data-tweet-id="515380319977893890"data-item-id="515380319977893890" data-name="" data-user-id="2665607941" data-expanded-footer="<div class="js-tweet-details-fixer tweet-details-fixer"> <div class="entities-media-container js-media-container" style="min-height:0px"> </div> <div class="js-machine-translated-tweet-container"></div> <div class="js-tweet-stats-container tweet-stats-container "> </div> <div class="client-and-actions"> <span class="metadata"> <span>11:00 PM - 25 Sep 2014</span> &middot; <a class="permalink-link js-permalink js-nav" href="/status/515380319977893890" tabindex="-1">Details</a> </span> </div> </div> " data-you-follow="false"data-you-block="false">" ;
Becomes this: 变成这个:
echo "<div class=\"tweet original-tweet js-stream-tweet js-actionable-tweet js-profile- popup-actionable js-original-tweet my-tweet\" data-tweet-id=\"515380319977893890\" data-item-id=\"515380319977893890\" data-name=\"\" data-user-id=\"2665607941\" data-expanded-footer=\"<div class="js-tweet-details-fixer tweet-details-fixer"> <div class="entities-media-container js-media-container" style="min-height:0px"> </div> <div class="js-machine-translated-tweet-container"></div> <div class="js-tweet-stats-container tweet-stats-container "> </div> <div class="client-and-actions"> <span class="metadata"> <span>11:00 PM - 25 Sep 2014</span> &middot; <a class="permalink-link js-permalink js-nav" href="/status/515380319977893890" tabindex="-1">Details</a> </span> </div> </div> \" data-you-follow=\"false\" data-you-block=\"false\">" ;
For a more advanced trick, you can move your entire HTML to a separate file, an HTML template if you will. 有关更高级的技巧,可以将整个HTML移到一个单独的文件(如果需要)中的HTML模板。 Then read in the file contents via PHP and find/replace the variables or dynamic parts as you need. 然后通过PHP读取文件内容,并根据需要查找/替换变量或动态部件。 This may be beyond the scope of your question however. 但是,这可能超出了您的问题范围。
回声中的引号需要转义:
echo "<div class=\"stream-item-footer\">";
用\\转义引号
echo "<div class=\"content\">" ;
Works for me: 为我工作:
echo '<div class="content">';
echo '<div class="stream-item-header">';
echo '<a class="account-group js-account-group js-action-profile js-user-profile-link js-nav" data-user-id="2665607941">
<img class="avatar js-action-profile-avatar" src="https://pbs.twimg.com/profile_images/506790785572814848/vdj_UAuZ_bigger.jpeg" alt="">
<strong class="fullname js-action-profile-name show-popup-with-id" data-aria-label-part> </strong>
<span>‏</span><span class="username js-action-profile-name" data-aria-label-part><s></s><b> </b></span>
</a>';
echo '<small class="time">
<a class="tweet-timestamp js-permalink js-nav js-tooltip" title="11:00 PM - 25 Sep 2014" ><span class="_timestamp js-short-timestamp "
data-aria-label-part="last" data-time="1411711218" data-time-ms="1411711218000" data-long-form="true">Sep 25</span></a>
</small>';
echo '</div>';
echo '<p class="js-tweet-text tweet-text" lang="en" data-aria-label-part="0"> </p>';
echo '<div class="stream-item-footer">';
echo '<ul class="tweet-actions js-actions">';
echo '<li class="more-tweet-actions">';
echo '<div class="action-more-container">';
echo '<div class="dropdown">';
echo '<button type="button" class="btn-link with-icn dropdown-toggle js-dropdown-toggle js-tooltip">';
echo '<span class="Icon Icon--delete"></span>';
echo '<b>Delete</b>';
echo '</button>';
echo '<div class="dropdown-menu">';
echo '<div class="dropdown-caret">';
echo '<div class="caret-outer"></div>';
echo '<div class="caret-inner"></div>';
echo '</div>';
echo '</div>';
echo '</div>';
echo '</div>';
echo '</li>';
echo '</ul>';
echo '</div>';
echo '</div>';
echo '</div>';
I'l write here my response dude, and hope i'm helping you. 我会在这里写下我的回答,希望我能为您提供帮助。 But there's nothing new, as Scott say, you missed a lot of double quote and IMHO you're a crazy boy doing it manually (in good sense). 但是,正如Scott所说的,没有什么新鲜的东西了,您错过了很多双引号,恕我直言,您是一个疯狂的男孩,手动操作(从某种意义上来说)。
Take you're JSON array and use the function i've linked in the comment in a way like this(hoping you have a similar situation, in other case is the same but you have to complex the code, maybe): 假设您是JSON数组,并使用我在注释中链接的函数,方法是这样的(希望您遇到类似的情况,在其他情况下是相同的,但是可能需要复杂的代码):
....
for($i=0; $i<sizeof($jsonarray); $i++){
echo addslashes($jsonarray[$i]);
}
....
In this way all of your code it's escaped, and if the error persist it means that's the problem is elsewhere 这样,您的所有代码都可以转义,如果错误仍然存在,则意味着问题出在其他地方
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.