简体   繁体   English

nl2br无法在ical摘要换行符“ \\ n”上使用

[英]nl2br not working on ical summary line feeds “\n”

I'm collecting a summary of an ical event in PHP. 我正在收集PHP中的ical事件的摘要。 The thing is, that the summary contains line breaks \\n and I want to replace them with <br> when inserting the events. 事实是,摘要包含换行符\\n并且我想在插入事件时将它们替换为<br>

In my PHPMyAdmin after escaping the ical summary I see the characters \\n , but without escaping the ical summary I can't see the characters \\n . 在转义摘要后,在我的PHPMyAdmin中,我看到了字符\\n ,但是在未转义摘要中,我看不到字符\\n However, without escaping I can see actual real line-breaks. 但是,如果不逃避,我可以看到实际的换行符。 I need to escape the ical summary to make my database safe. 我需要对摘要进行转义,以确保数据库安全。 Using the nl2br function is not working in both cases? 在两种情况下都无法使用nl2br函数吗? Why? 为什么?

CODE: 码:

//without escaping
$title = $vevent->getProperty('summary');//Object method which retrieves the summary of an event 
$title = nl2br($title);

//with escaping
$title = mysql_real_escape_string($vevent->getProperty('summary'));
$title = nl2br($title);

Hrm, what about reading the handbook page, my friend? 嗯,我的朋友,如何阅读手册页面? http://php.net/manual/en/function.mysql-real-escape-string.php says http://php.net/manual/en/function.mysql-real-escape-string.php

mysql_real_escape_string() calls MySQL's library function mysql_real_escape_string, which prepends backslashes to the following characters: \\x00, \\n, mysql_real_escape_string()调用MySQL的库函数mysql_real_escape_string,该函数将反斜杠添加到以下字符之前:\\ x00,\\ n,

Your line breaks are already SQL escaped. 您的换行符已经被SQL转义。 so you need to nl2br before. 因此,您需要先使用nl2br。

And the usual rant I give to everyone: why are you using the mysql extension in 2011? 我通常会给大家的咆哮:您为什么在2011年使用mysql扩展? It went out of fashion half a decade ago. 半年前,它就过时了。 Use mysqli or PDO and prepared statements, then you dont need to worry about escaping. 使用mysqli或PDO以及准备好的语句,那么您就不必担心转义。

You can do it the other way around: 您可以用另一种方法来做:

//with escaping
$title = nl2br($title);
$title = mysql_real_escape_string($vevent->getProperty('summary'));

But you shouldn't need to call nl2br before inserting into the database. 但是插入数据库之前,您不需要调用nl2br。 It's better to do nl2br when you output the data to the browser. 将数据输出到浏览器时最好执行nl2br。 That way you store the actual data in the database (which later can be used in other context) and format it with HTML before outputting. 这样,您可以将实际数据存储在数据库中(以后可以在其他上下文中使用),并在输出之前使用HTML对其进行格式化。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM