Code:
function utoh(text, min = 127, max = 255) { /* Ray Yates 3/2022 utoh() = Unicode to Html Translate diacritic and unicode characters to printable characters. Diacritic like 'áàâäãéèëêíìïîóòöôõúùüûñçăşţ' etc. and Unicode like © Given a string that contain special characters, converts characters to plain text or html code. (hex version) Examples: á becomes a, ç becomes c \u0092 becomes ’ ( displays the curled single quote ’ ); \u00A9 becomes © ( displays the copyright symbol © ); Usage: 1. let domObject = document.querySelector("#tab-Products"); domObject.innerHTML = utoh("&mvt:product:descrip;"); 2. $("#tab-Products").html( utoh(data.description) ); 3. $("#tab-Products").html( utoh(data.description, 150, 160) ); Limit unicode characters to replace. Paramiters: text: the string to clean up. min, max: optional unicode characters to search for. If omitted, defaults set to 127, 255 See: https://www.htmlsymbols.xyz/unicode for unicode character set. */ let norm_text = text.normalize("NFKD"); for (let index = min; index <= max; index++) { norm_text = norm_text.replaceAll( String.fromCodePoint(index), `&#x${index.toString(16).toUpperCase()};` ); } return norm_text; }
Leave a comment: