Unicode text?

Discussion in 'PHP' started by t3od0r, Sep 27, 2009.

  1. #1
    I need some help here, so i have this file:
    
       ▄█████▄ ░█████▄    ▄████▄░█▄ ░█▄  ▄█████▄ ███▄██ ▄██  ▄█▄  ▄██████ ▄█▄ ▄███
     ░████░██▀████▓ ▀██  ▄██░▀█████ ████ ███▓ ██████▓██ ▓███████ ███▓░▀█▀████ ███▓
     ███▓     ▓███░  ██  ██▄ ▄██▓██ ▓███▓██░    ▓███░███░███████████▄     ███░███░
     ███░ ▄███ ███ ▄█▀░ ████████░██  █████████▄  ▓██░███ ███▓█████████    ███████
     ███  ███░ ███████  ███▓ ░██ ██  ██████▓     ░██ ▀██ ███░███▓██████   ███▓███
     ███  ███  ████████░███░ ███ ██▄ ██▓███░      ██▄ ██░██▓ ███  ▀▀████  ███░███
     ███  ███  ███▓░███████░ ███ ███▓██ ███       ███▓██▓██░ ███    ▓████ ███ ███
     ▓██▓ ███  ███░ ▓██████  ███ ▓████  ▓██▄      ▓████████  ███     ▓███ ███ ███
      ███░███  ███  ░██████  ███ ░████  ░███       ███▓███▓  ███     ░███ ███ ███
       ██▓███  ███   ███▓██  ███  ███    ▓██▄      ███░███░  ███      ███ ███ ███
       ░█████  ███   ██▓░██▄ ███  ▓█▀     ▓██▄     ███ ██░   ███      ██▓ ███ ███
        ░███▀  ░█▀   ██░  ██ ░█▀           ░███▄    ▀  ▀     ▓█▀     ███░ ░█▀ ██▀
         ██░       ▄██▀   ░█▄                                      ▄███▀     ░██
       ░ ██       ▓░                                                ▓░       ██░
         ▓█░    ░█▀                                                  ▀█░    ░█▓
    Code (markup):
    now i want to insert it to mysql table and than export it with php,

    how can i do this? i managed to insert it to table exactly how it looks in the code tag but when i try to show it from table with php i see only strange simbols,

    can anyone help me?
     
    t3od0r, Sep 27, 2009 IP
  2. caprichoso

    caprichoso Well-Known Member

    Messages:
    433
    Likes Received:
    10
    Best Answers:
    0
    Trophy Points:
    110
    #2
    Well. The trick is you have to use the same character encoding every time. In the first place, MYSQL varchar fields have a character encoding. MySQL connection has a character encoding and Apache issues HTML specifying character encoding in HTTP field.

    MySQL connection: bool mysql_set_charset ( string $charset [, resource $link_identifier ] )

    Apache configuration or .htaccess: AddDefaultCharset <charset>

    In example:
    PHP
    
    mysql_connection(...);
    mysql_set_charset('utf-8');
    
    Code (markup):
    .htaccess
    
    AddDefaultCharset UTF-8
    
    Code (markup):
     
    caprichoso, Sep 27, 2009 IP
  3. t3od0r

    t3od0r Well-Known Member

    Messages:
    334
    Likes Received:
    3
    Best Answers:
    3
    Trophy Points:
    155
    #3
    thanks for your answer,
    i added mysql_set_charset('utf-8');
    my table is utf8_unicode_ci
    i am on localhost so i can't add htaccess

    i managed to do something here but the problem is that when i output the code from table it show all code only to one line not on multi lines how it should be

    Edit: if i output the code in a textarea it show fine
     
    Last edited: Sep 27, 2009
    t3od0r, Sep 27, 2009 IP
  4. caprichoso

    caprichoso Well-Known Member

    Messages:
    433
    Likes Received:
    10
    Best Answers:
    0
    Trophy Points:
    110
    #4
    Are you running PHP from command line?

    Please copy the portion of PHP which is actually outputting the text. Use PHP tags in advanced mode when posting the code!
     
    caprichoso, Sep 27, 2009 IP
  5. premiumscripts

    premiumscripts Peon

    Messages:
    1,062
    Likes Received:
    48
    Best Answers:
    0
    Trophy Points:
    0
    #5
    Read the sticky, it has a section on unicode.
     
    premiumscripts, Sep 27, 2009 IP
  6. caprichoso

    caprichoso Well-Known Member

    Messages:
    433
    Likes Received:
    10
    Best Answers:
    0
    Trophy Points:
    110
    #6
    It seems to be that the problem isn't unicode any more. But, a situation with the HTML formatting.
     
    caprichoso, Sep 27, 2009 IP
  7. premiumscripts

    premiumscripts Peon

    Messages:
    1,062
    Likes Received:
    48
    Best Answers:
    0
    Trophy Points:
    0
    #7
    Oh yeah, sorry, didn't read the last post completely. Sounds like you just need to add nl2br($text) ?
     
    premiumscripts, Sep 27, 2009 IP
  8. caprichoso

    caprichoso Well-Known Member

    Messages:
    433
    Likes Received:
    10
    Best Answers:
    0
    Trophy Points:
    110
    #8
    That will do if you are running PHP with Apache and if you are adding a \n at the end of each line.
     
    caprichoso, Sep 27, 2009 IP
  9. t3od0r

    t3od0r Well-Known Member

    Messages:
    334
    Likes Received:
    3
    Best Answers:
    3
    Trophy Points:
    155
    #9
    db conn:
    $conn=mysql_connect($dbhost, $dbuser, $dbpass);
    mysql_query("SET character_set_results = 'utf-8', character_set_client = 'utf-8', character_set_connection = 'utf-8', character_set_database = 'utf-8', character_set_server = 'utf-8'", $conn);
    PHP:
    insert to db:
    <form name="form" action="" method="get" id="form" onsubmit="return false"> 
    <label>info</label><br /><textarea name="info" cols="60" rows="5"></textarea><br />
    </form>
    
    $info=$_POST['info'];
    mysql_query("insert into data (info) VALUES ('$info') ") or die(mysql_error());
    
    PHP:
    output:
    $query=mysql_query("select * from data where id = '$id'");
    $row = mysql_fetch_array($query);
    echo $row['info'];
    
    PHP:
    this is what i get:
     ▄█████▄ ░█████▄ ▄████▄░█▄ ░█▄ ▄█████▄ ███▄██ ▄██ ▄█▄ ▄██████ ▄█▄ ▄███ ░████░██▀████▓ ▀██ ▄██░▀█████ ████ ███▓ ██████▓██ ▓███████ ███▓░▀█▀████ ███▓ ███▓ ▓███░ ██ ██▄ ▄██▓██ ▓███▓██░ ▓███░███░███████████▄ ███░███░ ███░ ▄███ ███ ▄█▀░ ████████░██ █████████▄ ▓██░███ ███▓█████████ ███████ ███ ███░ ███████ ███▓ ░██ ██ ██████▓ ░██ ▀██ ███░███▓██████ ███▓███ ███ ███ ████████░███░ ███ ██▄ ██▓███░ ██▄ ██░██▓ ███ ▀▀████ ███░███ ███ ███ ███▓░███████░ ███ ███▓██ ███ ███▓██▓██░ ███ ▓████ ███ ███ ▓██▓ ███ ███░ ▓██████ ███ ▓████ ▓██▄ ▓████████ ███ ▓███ ███ ███ ███░███ ███ ░██████ ███ ░████ ░███ ███▓███▓ ███ ░███ ███ ███ ██▓███ ███ ███▓██ ███ ███ ▓██▄ ███░███░ ███ ███ ███ ███ ░█████ ███ ██▓░██▄ ███ ▓█▀ ▓██▄ ███ ██░ ███ ██▓ ███ ███ ░███▀ ░█▀ ██░ ██ ░█▀ ░███▄ ▀ ▀ ▓█▀ ███░ ░█▀ ██▀ ██░ ▄██▀ ░█▄ ▄███▀ ░██ ░ ██ ▓░ ▓░ ██░ ▓█░ ░█▀ ▀█░ ░█▓
    Code (markup):
    and this if i add nl2br:
     ▄█████▄ ░█████▄ ▄████▄░█▄ ░█▄ ▄█████▄ ███▄██ ▄██ ▄█▄ ▄██████ ▄█▄ ▄███
    ░████░██▀████▓ ▀██ ▄██░▀█████ ████ ███▓ ██████▓██ ▓███████ ███▓░▀█▀████ ███▓
    ███▓ ▓███░ ██ ██▄ ▄██▓██ ▓███▓██░ ▓███░███░███████████▄ ███░███░
    ███░ ▄███ ███ ▄█▀░ ████████░██ █████████▄ ▓██░███ ███▓█████████ ███████
    ███ ███░ ███████ ███▓ ░██ ██ ██████▓ ░██ ▀██ ███░███▓██████ ███▓███
    ███ ███ ████████░███░ ███ ██▄ ██▓███░ ██▄ ██░██▓ ███ ▀▀████ ███░███
    ███ ███ ███▓░███████░ ███ ███▓██ ███ ███▓██▓██░ ███ ▓████ ███ ███
    ▓██▓ ███ ███░ ▓██████ ███ ▓████ ▓██▄ ▓████████ ███ ▓███ ███ ███
    ███░███ ███ ░██████ ███ ░████ ░███ ███▓███▓ ███ ░███ ███ ███
    ██▓███ ███ ███▓██ ███ ███ ▓██▄ ███░███░ ███ ███ ███ ███
    ░█████ ███ ██▓░██▄ ███ ▓█▀ ▓██▄ ███ ██░ ███ ██▓ ███ ███
    ░███▀ ░█▀ ██░ ██ ░█▀ ░███▄ ▀ ▀ ▓█▀ ███░ ░█▀ ██▀
    ██░ ▄██▀ ░█▄ ▄███▀ ░██
    ░ ██ ▓░ ▓░ ██░
    ▓█░ ░█▀ ▀█░ ░█▓
    Code (markup):
     
    t3od0r, Sep 27, 2009 IP
  10. caprichoso

    caprichoso Well-Known Member

    Messages:
    433
    Likes Received:
    10
    Best Answers:
    0
    Trophy Points:
    110
    #10
    Well. HTML collapses leading whitespaces unless you use PRE tag.

    Try encolsing the output inside <pre></pre>

    
    <?
    $query=mysql_query("select * from data where id = '$id'");
    ?>
    <pre>
    <?
    while($row = mysql_fetch_array($query)) {
      echo $row['info'];
    }
    ?>
    </pre>
    
    PHP:
     
    caprichoso, Sep 27, 2009 IP
    t3od0r likes this.
  11. t3od0r

    t3od0r Well-Known Member

    Messages:
    334
    Likes Received:
    3
    Best Answers:
    3
    Trophy Points:
    155
    #11
    thanks a lot, is working
     
    t3od0r, Sep 28, 2009 IP