|
Posted by Jerry Stuckle on 03/20/06 05:11
weirdstuff wrote:
> Hi. I have this simple code:
>
> ===========================================
> ->Database query here
>
> (.. some code)
>
> $row=mysql_fetch_array($res);
>
> (...)
>
> $formatting2 = $row['formatting2"];
>
> (..)
>
> //Echo variable from DB
> echo "$formatting2";
>
> $formatting2 = "USERNAME: %1\$s \r\nSERIAL: %2\$s \r\n\r\n";
> //Echo same string - this time from an "inline" variable
> echo "$formatting2";
> ===========================================
>
> Ok, maybe that was overly complicated. I hope you follow anyway. Whats
> happening here is that I am getting a formatted string from a database
> which I throw in a variable. What you see here is my test case to
> visualize the problem. Now, when I echo this variable or pass it to
> printf/sprintf then I get a different result than if I "manually"
> create the variable inline in my php script.
>
> So, the first echo $formatting2 there will output:
>
> USERNAME: %1$s \r\nSERIAL: %2$s \r\n\r\n
>
> whilst the second shows the newlines correctly. Also printf/sprintf
> barfs if I pass the string I got from the DB.
>
> What the heck is going on here? Database returning some strange string
> format?
>
>
> Thanks :)
>
> Arni Johannesson
>
Arni,
No, this is operating as expected.
In PHP strings, "\n" is a newline character. However, when read in from
an external source, these are the characters "backslash" and "en". The
equivalent in PHP would be "\\n" - the backslash is escaped and the
letter "n" follows.
But this is how it should be. External data should *never* be language
dependent - what happens if, for instance, you wanted the characters
blackslash and en in some text? For PHP you'd use "\\n", but for COBOL
the data would have to be "\n".
So, what you have to do is change your formatting characters. Replace
"\\n" with "\n" and "\\r" with "\r", for instance.
It should resolve your problem.
Or, alternatively, put the actual characters themselves in the database.
--
==================
Remove the "x" from my email address
Jerry Stuckle
JDS Computer Training Corp.
jstucklex@attglobal.net
==================
Navigation:
[Reply to this message]
|