php html从元描述标签中删除换行符
我正在 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 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
我会告诉你我的看法,因为我一次又一次地遇到这种情况。
有时修剪
\r\n
、\n
有帮助,有时则没有。在少数情况下,我发现我必须像这样删除:
我从来没有好奇心去找出为什么它不需要 \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:
I never had the curiosity to find out why it doesn't want \r\n or \n, maybe i'll foind out now :)