Forum Moderators: coopster

Message Too Old, No Replies

PHP Print a formatted string.

         

chadodes

4:35 pm on Sep 8, 2011 (gmt 0)

10+ Year Member



Hello..

Here is what I'm trying to do:
I store this into my SQL server as a text or varchar:
DATA UPDATE USERINFO PIN=4141\tName=RemoteDude\tPri=0\tPasswd=4455\tCard=1231\tGrp=1\tTZ=6


I retrieve it and store it as a variable called $cmdtxt
why I try to print it.. it prints with all of the formatting commands (\t in staid of a TAB)..

Here are some things I have tried:

echo '<pre>';
printf ($cmdtxt);
echo '</pre>';


I want this to print with tab spaces between each value, so it looks like this:
DATA UPDATE USERINFO PIN=4141 Name=RemoteDude Pri=0 Passwd=4455 Card=1231 Grp=1 TZ=6


How can I go about this?

Whatever the answer it it also needs to be compatible with: header('Content-type: text/plain')

Ideas?

Thanks.

Readie

5:22 pm on Sep 8, 2011 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



It may be because \t is being stored as a literal. Should be able to format it for printing by doing:

echo str_replace('\t', "\t", $cmdtxt);

(note the single quotes and double quotes usage there)

penders

5:23 pm on Sep 8, 2011 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member Top Contributors Of The Month



The \t in your SQL statement ($cmdtxt) is just that, the literal characters backslash and 't'. These should have perhaps been stored as the actual tab character instead (1 less character as well) in the very beginning, but anyway...

You need to manually convert these character sequences into the real TAB character...

$cmdtxt = str_replace('\t', "\t", $cmdtxt);


...then just echo it, or whatever.

EDIT: *snap* As Readie says :)

chadodes

5:29 pm on Sep 8, 2011 (gmt 0)

10+ Year Member



Bingo! that worked perfectly!
As for coding it properly in the beginning.. I'm actually working with code generated from a hardware device.. and I don't quite have the power to tell these guys how to code :)

Thanks for your help!