Save Unicode Text File

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • lerougegorge
    New Member
    • Jun 2008
    • 2

    Save Unicode Text File

    When I pull a string from a UTF-8 encoded field in my SQL, I get a string like the following:

    "& #2342;& #2369;& #2312;"

    (I have inserted a space between "&" and "#" to stop this from displaying as international text, in my actual string there is no space).

    This is a string of escaped characters, length of the string is 21 but it is actually representing something (international text) that is 3 characters.

    Now I would like to save these 3 characters to a file. This file should be 6 (3 x 2) bytes long. But, of course, when I use fwrite with the above string I get a file of 21 bytes.

    I could parse the string to get the six hex values (23,42,23,69,23 ,12) that I would need to write, but I can't even get PHP to output true binary. The following:

    Code:
    $fh = fopen("text.txt", "wb");
    fwrite($fh, (int)32);
    Outputs the number 32 in ASCII, not one byte of value 0x20. This is despite the "b" open mode! Can anyone help me with this? Ultimate goal is to save the above characters as described. Thanks!
  • hsriat
    Recognized Expert Top Contributor
    • Jan 2008
    • 1653

    #2
    It seems you have somewhere done wrong encoding.

    Can you show the snippet of your code to let us locate the error?

    Comment

    • lerougegorge
      New Member
      • Jun 2008
      • 2

      #3
      Hi - Thanks. I just installed the mbstring library and that fixed the problem. Sorry for the confusion!

      Comment

      • hsriat
        Recognized Expert Top Contributor
        • Jan 2008
        • 1653

        #4
        Ok, that's great :)

        Comment

        Working...