gpt4 book ai didi

php - 如何在此代码中添加 nl2br 和 htmlentities

转载 作者:行者123 更新时间:2023-11-30 22:15:46 25 4
gpt4 key购买 nike

我需要一些帮助。如何将 nl2br() 和 htmlentities() 函数输出到我的代码中?两者都需要应用于“内容”,但 htmlentities 需要应用于“标题”。

<?php
$sql = "
SELECT
post.id AS postid, post.title AS title, post.created,
DATE_FORMAT(Created, '%d-%m-%Y') AS created,
post.content AS content, post.image AS image,
post.alttext AS alttext, blog.id, blog.blogname
FROM
post
LEFT JOIN
blog ON post.blogid = blog.id
WHERE
blogid = \"" .(int)$_GET['id'] . "\"
ORDER BY
postid DESC";

$results = $db->query($sql);

if ($results->num_rows) {
while ($row = $results->fetch_object()) {
echo "<hr>
<h3>{$row->title}</h3>
<p>{$row->created}</p>
<p>
<div id='blog-image'>
<img src='images/{$row->image}' alt='{$row->alttext}'>
</div>
{$row->content}
</p>";
}
} else {
echo 'No Results';
}
?>

最佳答案

您可以为此使用一个新变量:

if ($results->num_rows) {
while ($row = $results->fetch_object()) {
$title = htmlentities($row->title);
$content = nl2br(htmlentities($row->content));
echo "<hr>
<h3>{$title}</h3>
<p>{$row->created}</p>
<p>
<div id='blog-image'>
<img src='images/{$row->image}' alt='{$row->alttext}'>
</div>
{$content}
</p>
";
}
}

或者打断字符串:

if ($results->num_rows) {
while ($row = $results->fetch_object()) {
echo "<hr>
<h3>". htmlentities($row->title) ."</h3>
<p>{$row->created}</p>
<p>
<div id='blog-image'>
<img src='images/{$row->image}' alt='{$row->alttext}'>
</div>
". nl2br(htmlentities($row->content)) ."
</p>
";
}
}

关于php - 如何在此代码中添加 nl2br 和 htmlentities,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/38333531/

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