php html从元描述标签中删除换行符

发布于 2024-11-25 03:57:35 字数 718 浏览 3 评论 0原文

我正在 PHP 中从 MYSQL 数据库打印页面内容。我在数据库中有一个名为 $row['description'] 的文本字段,

当将用户输入存储到数据库中时,我只使用 mysql_real_escape_string() 所以我不编辑 html

问题是我使用数据库中的这个字段作为 html 页面元描述标记。因此,如果用户在描述中插入换行符,则会导致元描述标记内的页面源代码出现换行符。我尝试通过使用循环跳过 br 格式来删除换行符。我使用 nl2br 转换为 br。

$desc_array=(explode(" ", $row['description']));
for($i=0; $i < 17;$i++){
    if(nl2br($desc_array[$i])=="<br />" || nl2br($desc_array[$i])=="<br/>"){
        $i++;
    } 

时,出现换行符

\r\n\r\n

我检查了我的 SQL 文件,当我尝试检查这种格式

 $desc_array[$i])=="\r\n\r\n"
 $desc_array[$i])=="\r\n"
 $desc_array[$i])=="\n"
 $desc_array[$i])=="\r"

,但仍然在描述标记中打印换行符。有什么想法吗?

I am printing page content from a MYSQL db in PHP. I have a text field in db called $row['description']

When storing users input into db I only use mysql_real_escape_string() so I do not edit html

Problem is I use this field from db as the html pages meta description tag. So if user inserted line breaks in description they cause a line break in the source code of the page within the meta description tag. I tried to remove the line break by using a loop to skip an br formats. I use the nl2br to convert to br.

$desc_array=(explode(" ", $row['description']));
for($i=0; $i < 17;$i++){
    if(nl2br($desc_array[$i])=="<br />" || nl2br($desc_array[$i])=="<br/>"){
        $i++;
    } 

I checked my SQL file and the line breaks appear as

\r\n\r\n

I have tried to check for this format too like

 $desc_array[$i])=="\r\n\r\n"
 $desc_array[$i])=="\r\n"
 $desc_array[$i])=="\n"
 $desc_array[$i])=="\r"

but still line break gets printed in description tag. Any ideas?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

淡写薰衣草的香 2024-12-02 03:57:35
$string = str_replace(array("\r", "\n"), array('', ''), $string);
$string = str_replace(array("\r", "\n"), array('', ''), $string);
梦醒时光 2024-12-02 03:57:35

我会告诉你我的看法,因为我一次又一次地遇到这种情况。

有时修剪 \r\n\n 有帮助,有时则没有。
在少数情况下,我发现我必须像这样删除:

<?php
$desc = "This
is a description";
// this is just a line break [enter]
$newDesc = str_replace(" 
", " ", $desc); // witch outputs This is a description
?>

我从来没有好奇心去找出为什么它不需要 \r\n 或 \n,也许我现在就会发现:)

I will tell you my opinion as I encountered this sitioation over and over.

Sometimes trimming out \r\n, \n helps, sometimes not.
In few cases i found out that i have to remove like so:

<?php
$desc = "This
is a description";
// this is just a line break [enter]
$newDesc = str_replace(" 
", " ", $desc); // witch outputs This is a description
?>

I never had the curiosity to find out why it doesn't want \r\n or \n, maybe i'll foind out now :)

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文