FRIHOST FORUMS SEARCH FAQ TOS BLOGS COMPETITIONS
You are invited to Log in or Register a free Frihost Account!


Need to unicode a string





DanielXP
Hi,


I need to be able to unicode a string in php

Here is an example of what it needs to do
Input: 電腦只是處理數位
Output: 96fb816653ea662f8655740665784f4d


This is for a multi language support on a SMS script im working on.


Any help would be great

Dan
Peterssidan
Isn't the input already unicode? UTF-8?
DanielXP
Peterssidan wrote:
Isn't the input already unicode? UTF-8?


I have no idea tbh,

All i know is people could enter the following into a textbox "電腦只是處理數位 "

and i need a script to convert it to "96fb816653ea662f8655740665784f4d" to pass on,

They say the following "Convert your text to Unicode [UCS-2 encoding"


Dan
bukaida
Have you included the following header before starting your actual code ?

Code:
<?php
                 header("Content-Type: text/html; charset=UTF-8");
         ?>



Moreover, your database and connection string (if any) should also be unicode enabled.
DanielXP
bukaida wrote:
Have you included the following header before starting your actual code ?

Code:
<?php
                 header("Content-Type: text/html; charset=UTF-8");
         ?>



Moreover, your database and connection string (if any) should also be unicode enabled.


Hi,

I have that at the top of my code and its also in the html
Code:
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />


Atm im just trying to get it to unicode straight from variables.

Code:
$sms_body = "電腦只是處理數位";
$sms_body2 = mb_convert_encoding($sms_body, 'UCS-2', 'auto');
echo "<textarea>".$sms_body2."</textarea>";


This is currently outputting the following,

Quote:
���fS�f/�UtexOM


Dan
Peterssidan
I was able to make this work:
Code:
<html>
   <head>
      <meta http-equiv="Content-type" content="text/html;charset=UTF-8">
   </head>
   <body>
      <form method="POST">
         <input type="text" name="input" />
      </form>
      <?php
         $input = mb_convert_encoding($_POST['input'], 'UCS-2', 'auto');

         $output = "";
         for ($i = 0; $i < strlen($input); $i++)
         {
            $output .= sprintf("%02s", bin2hex($input[$i]));
         }

         echo $output;
      ?>
   </body>
</html>


It works to just write the text in the file directly but you have to make sure to save the file as UTF-8. I wrote the code directly into DirectAdmin that appearantly didn't save as UTF-8 so that's why I used a html form here to make sure the text is UTF-8.
DanielXP
Peterssidan wrote:
I was able to make this work:
Code:
<html>
   <head>
      <meta http-equiv="Content-type" content="text/html;charset=UTF-8">
   </head>
   <body>
      <form method="POST">
         <input type="text" name="input" />
      </form>
      <?php
         $input = mb_convert_encoding($_POST['input'], 'UCS-2', 'auto');

         $output = "";
         for ($i = 0; $i < strlen($input); $i++)
         {
            $output .= sprintf("%02s", bin2hex($input[$i]));
         }

         echo $output;
      ?>
   </body>
</html>


It works to just write the text in the file directly but you have to make sure to save the file as UTF-8. I wrote the code directly into DirectAdmin that appearantly didn't save as UTF-8 so that's why I used a html form here to make sure the text is UTF-8.


Thanks for the solution! Smile

How would i go about saving this to a database?

Should I store it as "電腦只是處理數位" or "96fb816653ea662f8655740665784f4d"?

Dan
Related topics
Web Hosting in Languages other than English
writing COM components in Perl
C++ Assignment! its about string.h
Disabling "This is a string with a $var"?
Any Charset into Unicode?
Black Holes Actually Green
Your online routine
string's theory!!
replace a string for a position of text
Replacement of hexadecimal values in PHP string
Create Tamil Webpages with unicode font
information about phpnuke don'nt accept unicode or latin1
Send Unicode email with Oracle
String theory
Reply to topic    Frihost Forum Index -> Scripting -> Php and MySQL

FRIHOST HOME | FAQ | TOS | ABOUT US | CONTACT US | SITE MAP
© 2005-2011 Frihost, forums powered by phpBB.