开发者

Special chars in single and double quoted strings

开发者 https://www.devze.com 2023-01-16 10:26 出处:网络
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;}}

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开发者_如何学JAVA 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


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.)


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.

0

精彩评论

暂无评论...
验证码 换一张
取 消