0

What are the reasons of "ł" not being entity-able ?

and how would one do this for characters in general which are not in get_html_translation_table ?

How is the content of get_html_translation_table specified/defined ?

(talking only utf-8 here)

// control with 'ö'
php > echo htmlentities("ö",ENT_COMPAT,'utf-8');
ö

// test with 'ł'
php > echo htmlentities("ł",ENT_COMPAT,'utf-8');
ł

check get_html_translation_table:

php > var_dump(implode(',',array_keys(
                 get_html_translation_table(HTML_ENTITIES))));

// produces
// (why is ł not there?):
string(843) "",&,<,>, ,¡,¢,£,¤,¥,¦,§,¨,©,ª,«,¬,­,®,¯,°,±,²,³,´,µ,¶,·,¸,¹,º,»,¼,½,¾
,¿,À,Á,Â,Ã,Ä,Å,Æ,Ç,È,É,Ê,Ë,Ì,Í,Î,Ï,Ð,Ñ,Ò,Ó,Ô,Õ,Ö,×,Ø,Ù,Ú,Û,Ü,Ý,Þ,ß,à,á,â,ã,ä,å,æ
,ç,è,é,ê,ë,ì,í,î,ï,ð,ñ,ò,ó,ô,õ,ö,÷,ø,ù,ú,û,ü,ý,þ,ÿ,Œ,œ,Š,š,Ÿ,ƒ,ˆ,˜,Α,Β,Γ,Δ,Ε,Ζ,Η
,Θ,Ι,Κ,Λ,Μ,Ν,Ξ,Ο,Π,Ρ,Σ,Τ,Υ,Φ,Χ,Ψ,Ω,α,β,γ,δ,ε,ζ,η,θ,ι,κ,λ,μ,ν,ξ,ο,π,ρ,ς,σ,τ,υ,φ,χ
,ψ,ω,ϑ,ϒ,ϖ, , , ,,,,,–,—,‘,’,‚,“,”,„,†,‡,•,…,‰,′,″,‹,›,‾,⁄,€,ℑ,℘,ℜ,™,ℵ,←,↑,→,↓,↔
,↵,⇐,⇑,⇒,⇓,⇔,∀,∂,∃,∅,∇,∈,∉,∋,∏,∑,−,∗,√,∝,∞,∠,∧,∨,∩,∪,∫,∴,∼,≅,≈,≠,≡,≤,≥,⊂,⊃,⊄,⊆,⊇
,⊕,⊗,⊥,⋅,⌈,⌉,⌊,⌋,〈,〉,◊,♠,♣,♥,♦"

PHP 5.6.12

birdspider
  • 3,034
  • 1
  • 16
  • 25
  • 1
    _“why is ł not there?”_ – because not every possible Unicode characters _has_ an entity defined by HTML. (Which entities are defined, also differs between HTML versions.) Why do you (think you) _need_ an entity for this character in the first place? You should be using UTF-8 as character encoding throughout your whole site – then you don’t need to use entities any more for such “special” characters. – CBroe Aug 17 '15 at 14:31
  • @CBroe: yes I'm very well aware about utf-8 in prod, it's just that I sadly don't rule the world and have to handle "bad" data - in this case export/import from and to "bad" storages. I somehow expected it to get encoded to numerical entity instead of named if no named defined. – birdspider Aug 17 '15 at 14:38

2 Answers2

2

You need to handle code as HTML 5 with the ENT_HTML5 flag.

echo htmlentities("ł", ENT_COMPAT | ENT_HTML5, 'utf-8');
Federkun
  • 36,084
  • 8
  • 78
  • 90
  • so 5.4.0+ only, and `get_html_translation_table(HTML_ENTITIES,ENT_COMPAT | ENT_HTML5, "utf-8")` covers 1510 chars - well I suppose thats all there is to it - thx – birdspider Aug 17 '15 at 14:34
-1

php 5.3... You can change this func below (ISO-8859-2, WIN-1250) for UTF-8

<?php
if (!function_exists('htmlentities_polish')) { function htmlentities_polish($string) {

    if (!$GLOBALS['msFunc']['htmlentities_polish']['entities']) {
        $ignore = str_split('ACELNOSXZacelnosxz');  // Right Alt + that letter, on 'polish programmers keyboard' (or tilde (~) + that letter)
        // polish characters: Ą Ć Ę Ł Ń Ó Ś Ź Ż ą ć ę ł ń ó ś ź ż
        foreach ($ignore as &$value) {
            if (stripos('ae', $value)!==FALSE) $value .= 'ogon';
            else if (strtolower($value)==='l') $value .= 'strok';
            else if (strtolower($value)==='z') $value .= 'dot';
            else $value .= 'acute';
            $value = '&'.strtr($value, 'Xx' /* Z, z acute */, 'Zz').';';
            // See also: https://www.w3schools.com/charsets/ref_utf_latin_extended_a.asp , https://www.w3schools.com/charsets/ref_utf_latin1_supplement.asp
        }
        unset($value);
        $GLOBALS['msFunc']['htmlentities_polish']['entities'] = $ignore;

        $iso = array(161,198,202,163,209,211,166,172,175,177,230,234,179,241,243,182,188,191);  // ISO-8859-2
        foreach ($iso as &$value) { $value = chr($value);}
        $GLOBALS['msFunc']['htmlentities_polish']['iso'] = $iso;
        unset($value);

        $win = array(165,198,202,163,209,211,140,143,175,185,230,234,179,241,243,156,159,191);  // WINDOWS-1250 (WINDOWS-EE)
        foreach ($win as &$value) { $value = chr($value);}
        $GLOBALS['msFunc']['htmlentities_polish']['win'] = $win;
        unset($value);
    }

    
    /* Convert "everything" (win and iso) ... */
    if (!$GLOBALS['msFunc']['htmlentities_polish']['isowin']) { // Note: in assumption, it is within first call of htmlentities_polish() and isset($win)===TRUE etc. !
        $GLOBALS['msFunc']['htmlentities_polish']['isowin']=$diff=array_merge($iso, array_diff($win, $iso));
        $flip_win = array_flip($win);
        for ($i=count($iso); $i<count($diff); $i++) {
            $GLOBALS['msFunc']['htmlentities_polish']['entities'][$i] = $GLOBALS['msFunc']['htmlentities_polish']['entities'][$flip_win[$diff[$i]]];
        }
    }
    
    return str_replace($GLOBALS['msFunc']['htmlentities_polish']['isowin'], $GLOBALS['msFunc']['htmlentities_polish']['entities'], $string);

    /* ...or charset checking - in 2 ways:
    $diff = array_diff($GLOBALS['msFunc']['htmlentities_polish']['iso'], $GLOBALS['msFunc']['htmlentities_polish']['win']); // characters different between ISO-8859-2 and WINDOWS-1250
    
    // (1) fast but stupid way
    foreach ($diff as $value) {
        if (strpos($string, $value)!==FALSE) return str_replace($GLOBALS['msFunc']['htmlentities_polish']['iso'], $GLOBALS['msFunc']['htmlentities_polish']['entities'], $string);
    }   // entities from ISO-8859-2 and return !
    // otherwise // entities from WINDOWS-1250 and return :
    return str_replace($GLOBALS['msFunc']['htmlentities_polish']['win'], $GLOBALS['msFunc']['htmlentities_polish']['entities'], $string);
    
    // (2) slow but exact way 
    foreach (str_split($string) as $value) {
        if (in_array($value, $diff)) $iso_c++;
    }
    $diff = array_diff($GLOBALS['msFunc']['htmlentities_polish']['win'], $GLOBALS['msFunc']['htmlentities_polish']['iso']);
    foreach (str_split($string) as $value) {
        if (in_array($value, $diff)) $win_c++;
    }
    if ($win_c>$iso_c) return str_replace($GLOBALS['msFunc']['htmlentities_polish']['win'], $GLOBALS['msFunc']['htmlentities_polish']['entities'], $string);
    else return str_replace($GLOBALS['msFunc']['htmlentities_polish']['iso'], $GLOBALS['msFunc']['htmlentities_polish']['entities'], $string);
    */
// polish characters: Ą Ć Ę Ł Ń Ó Ś Ź Ż ą ć ę ł ń ó ś ź ż
}
}
?>

Update: Look there.

echo utf_entities('ł Ł'); // &lstrok; &Lstrok;
msegit
  • 110
  • 8