gpt4 book ai didi

php - 在 PHP 代码中嵌入 HTML

转载 作者:行者123 更新时间:2023-11-28 04:54:08 24 4
gpt4 key购买 nike

我必须使用 REST Api 获取数据,并且我的响应是在 json 中。解码后响应在一个数组中,我必须在网页中显示它,以便数组中的每个响应都以特定的显示格式显示(就像推特中的推文以特定的框类型格式显示)。如何在 php 中循环遍历数组并显示它?我找到的唯一解决方案是使用 echo 将 HTML 嵌入 php,但它不起作用。而且调试器也没有太大帮助。我错过了什么?

请告知该怎么做。

这是我的代码:

    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="&lt;div class=&quot;js-tweet-details-fixer tweet-details-fixer&quot;&gt;&#10;&#10;&#10;&#10;    &lt;div class=&quot;entities-media-container js-media-container&quot; style=&quot;min-height:0px&quot;&gt;&#10;    &lt;/div&gt;&#10;&#10;  &lt;div class=&quot;js-machine-translated-tweet-container&quot;&gt;&lt;/div&gt;&#10;    &lt;div class=&quot;js-tweet-stats-container tweet-stats-container &quot;&gt;&#10;    &lt;/div&gt;&#10;&#10;  &lt;div class=&quot;client-and-actions&quot;&gt;&#10;  &lt;span class=&quot;metadata&quot;&gt;&#10;    &lt;span&gt;11:00 PM - 25 Sep 2014&lt;/span&gt;&#10;&#10;       &amp;middot; &lt;a class=&quot;permalink-link js-permalink js-nav&quot; href=&quot;/status/515380319977893890&quot;  tabindex=&quot;-1&quot;&gt;Details&lt;/a&gt;&#10;    &#10;&#10;&#10;  &lt;/span&gt;&#10;&lt;/div&gt;&#10;&#10;&#10;&lt;/div&gt;&#10;" 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>&rlm;</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>";

在 Scott 的回答后,我更改了我的代码,但它仍然无法正常工作,我也没有得到它。

 echo  "<div class=\"tweet original-tweet js-stream-tweet js-actionable-tweet js-profile-popup-actionable js-original-tweet   

我的推文\"data-tweet-id="515380319977893890\"data-item-id=\"515380319977893890\"data-name=\"\"data-user-id=\"2665607941\"data-expanded-footer=\"

2014 年 9 月 25 日下午 11:00 · 详情
“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>&rlm;</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>";

最佳答案

检查你的单引号和双引号。正在返回的字符串正在被截断。这是无效的:

 echo   "<div class="stream-item-header">" ;

将其更改为:

echo   "<div class=\"stream-item-header\">" ;

或者:

echo   "<div class='stream-item-header'>" ;

并对每条 echo 线重复此修复。

例如,这个:

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="&lt;div class=&quot;js-tweet-details-fixer tweet-details-fixer&quot;&gt;&#10;&#10;&#10;&#10;    &lt;div class=&quot;entities-media-container js-media-container&quot; style=&quot;min-height:0px&quot;&gt;&#10;    &lt;/div&gt;&#10;&#10;  &lt;div class=&quot;js-machine-translated-tweet-container&quot;&gt;&lt;/div&gt;&#10;    &lt;div class=&quot;js-tweet-stats-container tweet-stats-container &quot;&gt;&#10;    &lt;/div&gt;&#10;&#10;  &lt;div class=&quot;client-and-actions&quot;&gt;&#10;  &lt;span class=&quot;metadata&quot;&gt;&#10;    &lt;span&gt;11:00 PM - 25 Sep 2014&lt;/span&gt;&#10;&#10;       &amp;middot; &lt;a class=&quot;permalink-link js-permalink js-nav&quot; href=&quot;/status/515380319977893890&quot;  tabindex=&quot;-1&quot;&gt;Details&lt;/a&gt;&#10;    &#10;&#10;&#10;  &lt;/span&gt;&#10;&lt;/div&gt;&#10;&#10;&#10;&lt;/div&gt;&#10;" data-you-follow="false"data-you-block="false">" ;

变成这样:

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=\"&lt;div class=&quot;js-tweet-details-fixer tweet-details-fixer&quot;&gt;&#10;&#10;&#10;&#10;    &lt;div class=&quot;entities-media-container js-media-container&quot; style=&quot;min-height:0px&quot;&gt;&#10;    &lt;/div&gt;&#10;&#10;  &lt;div class=&quot;js-machine-translated-tweet-container&quot;&gt;&lt;/div&gt;&#10;    &lt;div class=&quot;js-tweet-stats-container tweet-stats-container &quot;&gt;&#10;    &lt;/div&gt;&#10;&#10;  &lt;div class=&quot;client-and-actions&quot;&gt;&#10;  &lt;span class=&quot;metadata&quot;&gt;&#10;    &lt;span&gt;11:00 PM - 25 Sep 2014&lt;/span&gt;&#10;&#10;       &amp;middot; &lt;a class=&quot;permalink-link js-permalink js-nav&quot; href=&quot;/status/515380319977893890&quot;  tabindex=&quot;-1&quot;&gt;Details&lt;/a&gt;&#10;    &#10;&#10;&#10;  &lt;/span&gt;&#10;&lt;/div&gt;&#10;&#10;&#10;&lt;/div&gt;&#10;\" data-you-follow=\"false\" data-you-block=\"false\">" ;

对于更高级的技巧,您可以将整个 HTML 移动到一个单独的文件中,如果您愿意,可以将其移动到一个 HTML 模板中。然后通过 PHP 读取文件内容并根据需要查找/替换变量或动态部分。但是,这可能超出了您的问题范围。

关于php - 在 PHP 代码中嵌入 HTML,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/26489351/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com