Pagina 1 di 3 1 2 3 ultimoultimo
Visualizzazione dei risultati da 1 a 10 su 24
  1. #1
    Utente bannato
    Registrato dal
    Feb 2011
    Messaggi
    146

    Problemi di codifica /uxxxx

    Ho delle stringhe cosi:

    (questi sono caratteri speciali http://www.fileformat.info/info/unic...r/e7/index.htm)

    "grazie per l'aiuto \u00e7 \u00e7 \u00e7 grazie \u20ac \u20ac \u221a"


    Io faccio un semplice replace, e aggiorno questo elenco, man mano che ne trovo altre..
    $string=str_replace('\u00e7', "ç", $string);
    $string=str_replace('\u201c', "“", $string);
    $string=str_replace('\u20ac', "euro", $string);
    $string=str_replace('\u221a', "√", $string);

    C'è un metodo alternativo, e univoco ?-?

    io non saprei
    vi è mai capitato?

  2. #2
    Utente di HTML.it L'avatar di Grino
    Registrato dal
    Oct 2004
    Messaggi
    739
    Dal manuale sul tipo string di PHP:
    Strings
    A string is series of characters, therefore, a character is the same as a byte. That is, there are exactly 256 different characters possible. This also implies that PHP has no native support of Unicode. See utf8_encode() and utf8_decode() for some basic Unicode functionality.
    Quindi le stringhe php non supportano direttamente UTF o charset multibytes (ma di questo te ne sei già accorto. Un primo consiglio del manuale è dare una occhiata alle funzioni utf8_encode e utf8_decode.

    Queste però ti applicano dei salti fra charset UTF8<->ISO-8859-1

    Più in generale dovresti dare una occhiata al manuale per le multibyte string

    Siamo sempre troppo gelosi delle nostre grandi piccole opere! - Grino inedito.
    Lavori e Lavoretti

  3. #3
    Utente bannato
    Registrato dal
    Feb 2011
    Messaggi
    146
    function unicode_decode($str){
    return preg_replace("/\\\u([0-9A-F]{4})/ie", "iconv('utf-16', 'utf-8', hex2str(\"$1\"))", $str);
    }

    function hex2str($hex) {
    $r = '';
    for ($i = 0; $i < strlen($hex) - 1; $i += 2)
    $r .= chr(hexdec($hex[$i] . $hex[$i + 1]));
    return $r;
    }

  4. #4
    Utente bannato
    Registrato dal
    Feb 2011
    Messaggi
    146
    li traduce si ma in maniera sballata

    ˙·٠•◗•٠·˙ ©
    al posto di
    ˙·٠•●●•٠·˙ ©

    starà sbagliato questo
    'utf-16', 'utf-8'

  5. #5
    Utente bannato
    Registrato dal
    Feb 2011
    Messaggi
    146

  6. #6
    Utente bannato
    Registrato dal
    Feb 2011
    Messaggi
    146
    forse dipende dal database?
    Attualmente ho:

    define('DB_CHARSET', 'ISO-8859-1');

    define('DB_COLLATE', 'latin1_bin');

  7. #7
    Forse dipende dal fatto che hai copiato male la funzione che ti avevo dato tempo fa??
    codice:
    function unicode_decode($str){
        return preg_replace("/\u([0-9A-F]{4})/ie", "hex2str(\"$1\")", $str);
    }
    
    function hex2str($hex) {
        $r = '';
        for ($i = 2; $i < strlen($hex) - 1; $i += 2){
            $r .= chr(hexdec($hex[$i] . $hex[$i + 1]));
        }
        return $r;
    }
    
    $text = "Grazie mille per l'aiuto, u00ec u0031 u00fc u00e8";
    $stringa_de_codificata = unicode_decode($text);
    echo $stringa_de_codificata;
    Non appropriarti delle soluzioni altrui (tra l'altroriportandole errate) senza nemmeno degnarti di citare l'autore...
    http://forum.html.it/forum/showthrea...2#post13254072

  8. #8
    Utente bannato
    Registrato dal
    Feb 2011
    Messaggi
    146
    Ma purtroppo non mi funziona

    Codice PHP:
    function unicode_decode($str){
        return 
    preg_replace("/\u([0-9A-F]{4})/ie""hex2str(\"$1\")"$str);
    }

    function 
    hex2str($hex) {
        
    $r '';
        for (
    $i 2$i strlen($hex) - 1$i += 2){
            
    $r .= chr(hexdec($hex[$i] . $hex[$i 1]));
        }
        return 
    $r;
    }


    function 
    pulisci($string) {
    $string=json_encode($string);
    echo 
    "partenza:" $string "
    </br>"
    ;

    $string unicode_decode($string);

    echo 
    "decodificata:" $string "
    </br>"
    ;

        
    $string=substr($string0, -1);
        
    $string=substr($string1);

        return 
    $string;

    ECHO
    partenza:"\n\n\u00b0.\u2022\u00b0\u2022.\u2605dive nta fan\u2605.\u2022\u00b0\u2022.\u00b0 \u00a9"

    decodificata:

    non mi restituisce niente??

  9. #9
    Piuttosto normale....

    Warning: preg_replace() [function.preg-replace]: Compilation failed: PCRE does not support \L, \l, \N, \U, or \u at offset 1

    Se il risutato dovrebbe essere tipo questo: °."°".diventa fan."°".° © allora c'è solo da fae un piccolo cambio...

    codice:
    ....
    return preg_replace("#/u([0-9A-F]{4})#ie", "hex2str(\"$1\")", $str);
    ....
    $text = "\u00b0.\u2022\u00b0\u2022.\u2605diventa fan\u2605.\u2022\u00b0\u2022.\u00b0 \u00a9";
    $text = str_replace("\u", "/u", $text);
    ....

  10. #10
    Utente bannato
    Registrato dal
    Feb 2011
    Messaggi
    146
    Codice PHP:
    function unicode_decode($str){
    return 
    preg_replace("#/u([0-9A-F]{4})#ie""hex2str(\"$1\")"$str);
    }

    function 
    hex2str($hex) {
        
    $r '';
        for (
    $i 2$i strlen($hex) - 1$i += 2){
            
    $r .= chr(hexdec($hex[$i] . $hex[$i 1]));
        }
        return 
    $r;
    }


    function 
    pulisci($string) {
    $string=json_encode($string);
    $string str_replace("\u""/u"$string);

    echo 
    "partenza:" $string "
    </br>"
    ;

    $string unicode_decode($string);

    echo 
    "decodificata:" $string "
    </br>"
    ;

        
    $string=substr($string0, -1);
        
    $string=substr($string1);

        return 
    $string;


    Qualcosa cambia..
    partenza:"\n\n/u00b0./u2022/u00b0/u2022./u2605diventa fan/u2605./u2022/u00b0/u2022./u00b0 /u00a9"

    decodificata:"°."°".diventa fan."°".° ©"

    L'originale è °.•°•.★diventa fan★.•°•.° ©
    Su un sito che la visualizza bene c'è
    <meta http-equiv="Content-type" content="text/html; charset=utf-8" /> (come il mio sito)

Permessi di invio

  • Non puoi inserire discussioni
  • Non puoi inserire repliche
  • Non puoi inserire allegati
  • Non puoi modificare i tuoi messaggi
  •  
Powered by vBulletin® Version 4.2.1
Copyright © 2025 vBulletin Solutions, Inc. All rights reserved.