views:

28

answers:

2

Hey!

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

+1  A: 

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

Messa
The RTF is stored in a Blob field on a MSSQL Database. I retrieve the rows via mssql_fetch_assoc...And when it`s printed all occurences of \r or \n are interpreted as these... not as plain chars...
rgn
Do you mean that you want to print \r / \n without being interpreted like special characters?
clinisbut
yes.if you take a look at the start of the orginial string it should look like: {\rtf1... in hex => 0x7b 0x5c 0x72 0x74 0x66but in the string hold by php it looks in hex like this => 0x7b 0xd7 0x46 0x63 0x15
rgn
okay thanks for you help, it seems like the problem was caused by my test case... i get the correct string from my datase. but my soup serializer have trouble with the special chars... mea culpa.
rgn
+1  A: 

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.

clinisbut
stripsplashes would not help, becaus i`m would like to convert this RTF text to plain text. So the keywords like \rtf1 or \ansi should persist for the parser.
rgn