-1

I m getting a request, when i use echo it prints it out on the shell and everything is awesome.

I want to write human readable string to file.

When i try to write these messages to file. i see binary string.

like this:

6^@8^@3^@7^@8^@1^@0^@,^@,^@1^@1^@b^@c^@d^@9^@0^@f^@-^@e^@3^@3^@a^@-^@4^@e^@0^@5^@-^@9^@e^@3^@d^@-^@5^@f^@a^@1^@8^@3^@4^@f^@d^@5^@e^@7^@,^@,^@:^@:^@1^@,^@M^@o^@z^@i^@l^@l^@a^@/^@5^@.^@0^@ ^@(^@W^@i^@n^@d^@o^@w^@s^@ ^@N^@T^@ ^@6^@.^@1^@;^@
 ^@W^@O^@W^@6^@4^@;^@ ^@r^@v^@:^@9^@.^@0^@.^@1^@)^@ ^@G^@e^@c^@k^@o^@/^@2^@0^@1^@0^@0^@1^@0^@1^@ ^@F^@i^@r^@e^@f^@o^@x^@/^@9^@.^@0^@.^@1^@,^@F^@i^@r^@e^@f^@o^@x^@,^@9^@.^@0^@,^@W^@i^@n^@d^@o^@w^@s^@,^@,^@0^@,^@0^@,^@0^@,^@0^@,^@0^@,^@,^@
 ^@:^@:^@1^@,^@ ^@1^@/^@2^@1^@/^@2^@0^@1^@2^@ ^@5^@:^@4^@5^@:^@5^@0^@ ^@P^@M^@

but i expect this:

6837817,,01497aed-181b-4054-b68f-3b41b4e707fc,,::1,Mozilla/5.0 (Windows NT 6.1; WOW64; rv:9.0.1) Gecko/20100101 Firefox/9.0.1,Firefox,9.0,Windows,,0,0,0,0,0,, ::1, 1/21/2012 5:45:51 PM

ok i saw that there are methods like pack and unpack to get string from binary string but i failed.

<?php

$context = new ZMQContext();
$receiver = new ZMQSocket($context, ZMQ::SOCKET_PULL);
$receiver->bind("tcp://*:5557");

$fp = fopen('data.txt', 'w');

while(true){ 
$str = $receiver->recv();

echo $str."\n";

file_put_contents('foo.txt', $str);

//fwrite($fp, $str."\n");


}
fclose($fp);
?>

How can i get this done? I just wanted to write the string to file:(

Also, is this an expensive operation?

11
  • Why are you (un)packing to begin with? What is that string? What's wrong with simply writing it to a file?
    – deceze
    Jan 22, 2012 at 1:43
  • i see crap . i mean binary when i write to file. when i print out with echo, i get string representation but not in a file.
    – DarthVader
    Jan 22, 2012 at 1:44
  • What exactly do you see in the file? And more importantly, how are you checking what's in the file? And what do you expect to see?
    – deceze
    Jan 22, 2012 at 1:47
  • @deceze see edit. I m checking with less
    – DarthVader
    Jan 22, 2012 at 1:48
  • Why don't you just skip the packing and just write the string to the file?
    – deceze
    Jan 22, 2012 at 1:51

2 Answers 2

1

What you have there isn't binary data, but a different encoding for text. It's some form of UCS-2 I'd say. So you can "decode" it with:

 $string = iconv("UCS-2LE", "UTF-8", $string);
 file_put_contents(...);

See iconv and UCS-2, BOM FF FE.

0
1

What's wrong with file_put_contents? http://php.net/file_put_contents

file_put_contents('file/path.ext', $str);

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.