tags:

views:

560

answers:

6

I have some data that won't printf....

echo works, but not printf

There is data in the string, also simplexml_load_string won't parse the string.

There is some binary or invisible character that I cannot see and can't get the ordinal value for that is causing printf to fail as well as most other string functions in PHP.

Clearly this is an encoding issue, however, I don't know how to find out what it is so I can properly encode/decode it.

$string = getStringFromDatabase();
printf($string);

...

Nothing is displayed

...

No errors, No Warnings, No Nothing

Clearly there is data there, I know it's there if I loop over every single character, however, I don't where to go from there.

[UPDATE] This is weird because the ordinal value is outside of the typical ASCII range.

ord($buffer[$i]) = 1610

it is a combo of a DLE and LF characters. Not sure how it got there or why. Or what it was supposed to be, the data on either side is valid and the location for this is quite odd.

think <offending char here>0,000

+2  A: 

echo will output a string literally, but if you pass your string as the first parameter to printf, it is interpreted as format - if it contains % characters, these may be the source of your problem.

If this works:

echo $foo

then so should this:

printf("%s", $foo);

If you want to get a quick and dirty hex dump of a string, try something like

$foo="fo \00 33jj";

$len=strlen($foo);
for ($i=0; $i<$len; $i++)
{
  printf("%02x ", ord($foo[$i]));
}

//outputs 66 6f 20 00 20 33 33 6a 6a
Paul Dixon
If I could print out the data, then I wouldn't be asking this! lolActually, it's medical data and so it's secret. If I could find the offending character, then I could fix it.This is why I'm asking...freaking stuck.
The Chairman
+1  A: 

Look at here: http://ditio.net/2008/11/04/php-string-to-hex-and-hex-to-string-functions/ and perhaps just display the hex representation, and the bad characters may come out, but; you can also just go through the string and copy all the printable characters (ASCII > 31 and < 128) into a new string and then print out that string.

James Black
A: 

Try

file_put_contents('tmp.txt', $string);

then load it in a hex editor.

Greg
A: 

At that point I would try as the string could be empty:

echo "'" . $string . "'<br>";
jmucchiello
+5  A: 

Definition of printf:

int printf  ( string $format  [, mixed $args  [, mixed $...  ]] )

Isn't your problem simply that you are passing your string as $format? Don't you need to use simply print or provide printf with a $format string AND a $string?

Peter Perháč
+1  A: 

Thanks for the not-funny, wise-crack non-answers and comments that are far worse than my poorly stated question. I guess, a lot of people are rep-trolls looking for the easy questions to answer instead of having to think. Feel free to down vote this too.

Sincere thanks to the people who actually tried to help.

For those of you who encounter the same issue, here's the real answer:

The answer is a multi-byte character that is an bug from the runtime. You can work around it by finding chr(16) in your string and replacing it with an empty string (""). That is if you need it to work with s/v/printf functions.

The Chairman