单引号和双引号字符串中的特殊字符

发布于 2024-09-19 21:41:31 字数 488 浏览 3 评论 0原文

我从包含 rtf 文档的数据库中获取一个字段。

例如,这可能如下所示:

{\rtf1\ansi\ansicpg1252\deff0\deflang1031{\fonttbl{\f0\fnil\fcharset0 Calibri;}} {*\generator Msftedit 5.41.21.2509;}\viewkind4\uc1\pard\sa200\sl276\slmult1\lang7\f0\fs22 asdfasdf\par 一个\标准杆 SF\标准杆 asd\par 法斯德\帕 \b dfas\b0\par dfas\par }

现在 PHP 从数据库中以双引号的形式获取该字符串,结果是该字符串不会被按字符解释...假设像 '\r' 和 '\n' 这样的特殊字符被识别。

我如何从这个双引号转换为单引号字符串,以便我获得所有原始字符?或者,当我从数据库中获取该值时,如何才能将该值指定为单引号?

提前致谢

-拉尔夫

I fetch a field from a database that contains a rtf document.

For Example this could look like this:


{\rtf1\ansi\ansicpg1252\deff0\deflang1031{\fonttbl{\f0\fnil\fcharset0 Calibri;}}
{*\generator Msftedit 5.41.21.2509;}\viewkind4\uc1\pard\sa200\sl276\slmult1\lang7\f0\fs22 asdfasdf\par
a\par
sf\par
asd\par
fasd\par
\b dfas\b0\par
dfas\par
}

Now PHP fetches this as double quoted from the database, the result ist that the string will not be interpreded char wise... assumed special chars like '\r' and '\n' got recognized.

How can i convert from this double quoted to a single quoted string so that i got all raw chars? Or how can i achieve that the value is asigned as single quoted when i fetch it from database?

Thanks in advance

-ralf

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

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

发布评论

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

评论(2

相对绾红妆 2024-09-26 21:41:31

现在 PHP 将其作为双引号获取
来自数据库

什么? mysql_fetch_row 或whatewer 的结果只是一个字符串。没有任何东西以任何方式被重新解释。 \n 仍然是 \n。只有您在 PHP 文件中写入双引号的字符串文字才会被“解释”,然后存储为字符串。

没有什么比单引号或双引号字符串更好的了。 PHP 源代码中只有单引号或双引号字符串文字,从中生成实际的 PHP 字符串。

您现在遇到的唯一问题是如何处理/解析 RTF 数据< /a>. (假设数据存储在 blob 列中,因此字符编码不会变得复杂。)

Now PHP fetches this as double quoted
from the database

What? The result of mysql_fetch_row or whatewer is just a string. Nothing is reinterpreted in any way. \n just stays \n. Only string literals you write in the PHP file into double quotes will be "interpreted" and then stored as a string.

There is nothing like single- or double-quoted string. There are just single- or double-quoted string literals in the PHP source code from which the actual PHP strings will be made.

The only problem you have now is how to process/parse the RTF data. (Assuming the data was stored in blob column so there is no complication with character encodings.)

云仙小弟 2024-09-26 21:41:31

首先,您应该投入一些时间来研究谁(或什么)正在转义您的代码。

但为了快速解决方案,请尝试使用 stripslashes() 函数:

$unsecaped = stripslashes( $database_data );

但我强烈建议您尝试查找转义数据的内容。
这可能发生在:

  • 将数据插入数据库之前。这通常是由 PHP 指令 magic_quotes_gpc 引起的。
  • 从数据库中检索数据时。

已更新

我不明白你的问题...
您想保留所有这些反斜杠,但避免 \r 和 \n 被解释为回车符和换行符...

尝试执行 str_replace 来查找所有这些 \r 和 \n 并将它们替换为 \r 和 \n。

我不知道 \r 是否可以属于任何明智的字符,所以也许你应该只替换 " \r
"/" \n ",您可能需要 preg_replace() 。

First of all you should invest some time who (or what) is escaping your code.

But for a quick solution, try to use the stripslashes() function:

$unsecaped = stripslashes( $database_data );

But I urge you try to find what is escaping the data.
This can occur:

  • Before inserting the data into database. This is typically caused by the PHP directive magic_quotes_gpc.
  • When retrieving the data from database.

Updated

I didn't understand your problem...
You want to keep all those backslashes but avoid to \r and \n being interpreted as carriage return and line feed...

Try to do a str_replace to find all those \r and \n and replacing them with \r and \n.

I don't know if \r could belong to any wise char, so maybe you should replace only " \r
"/" \n ", You'll need preg_replace() for this possibly.

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