Home > Net >  How to convert UNICODE characters within a string, to their string representation?
How to convert UNICODE characters within a string, to their string representation?

Time:05-27

Basically I have these strings

var String1 = 'Concepciu00f3n';
var String2 = 'u00d1eembucu00fa';

I want to convert them back so that they will look like this:

var String1 = 'Concepción';
var String2 = 'Ñeembucú';

I've been searching a lot but I haven't found a solution in js yet

Edit: This is how the data is stored in my DB

$.ajax({
            url: 'inc/datos-graficos-data',         
            dataType : "json",
            type: 'POST',
            data: {
                q: 'guardar_grafico',
                id: id,
                headers: headers,
                datos: save_data,
                meta: editor,
                tipo: tipo_grafico,
                array: selectedRows,//This is the array with all the strings that gets saved with unicodes
                excel: excel_format
            },
            async: false,
            beforeSend: function() {
                //$("#ventasGrafico").html("<img src='images/loading.gif'>");
            },
            success: function(json) {
                $('#modal_parseo').modal('hide');
                //SWAL
                Swal.fire(
                'Exito!',
                json,
                'success'
                ).then((result) => {
                    // Reload the Page
                    location.reload();
                });
            },
            cache: true
        });

This is what my selectedRows array looks like before I send it via ajax, note that some of the values on the "agrupacion" keys are strings with special characters like "Asunción" or "Ñeembucú".

const selectedRows = [
  {agrupacion: 'Asunción', col2015: 0, col2016: 0, col2017: 0, col2018: 0},
  {agrupacion: 'Total', col2015: 105.1, col2016: 108, col2017: 112.2, col2018: 106.9},
  {agrupacion: 'Hombre', col2015: 103, col2016: 106.7, col2017: 115, col2018: 106.8},
  {agrupacion: 'Mujer', col2015: 107.2, col2016: 109.3, col2017: 109.4, col2018: 107.1},
  {agrupacion: 'Concepción', col2015: 0, col2016: 0, col2017: 0, col2018: 0},
  {agrupacion: 'Total', col2015: 77.8, col2016: 79.4, col2017: 82.9, col2018: 74.3},
  {agrupacion: 'Hombre', col2015: 76.4, col2016: 80.2, col2017: 81.3, col2018: 71.5},
  {agrupacion: 'Mujer', col2015: 79.3, col2016: 78.5, col2017: 84.6, col2018: 77.2},
  {agrupacion: 'San Pedro', col2015: 0, col2016: 0, col2017: 0, col2018: 0},
  {agrupacion: 'Total', col2015: 76.8, col2016: 77.7, col2017: 77.2, col2018: 70.2},
  {agrupacion: 'Hombre', col2015: 77.2, col2016: 79.1, col2017: 79.8, col2018: 69.5},
  {agrupacion: 'Mujer', col2015: 76.3, col2016: 76.3, col2017: 74.5, col2018: 70.9},
  {agrupacion: 'Cordillera', col2015: 0, col2016: 0, col2017: 0, col2018: 0},
  {agrupacion: 'Total', col2015: 77.9, col2016: 77.5, col2017: 75.6, col2018: 67.5},
  {agrupacion: 'Hombre', col2015: 76.4, col2016: 78.1, col2017: 73.7, col2018: 67.5},
  {agrupacion: 'Mujer', col2015: 79.5, col2016: 76.7, col2017: 77.6, col2018: 67.5},
  {agrupacion: 'Guairá', col2015: 0, col2016: 0, col2017: 0, col2018: 0},
  {agrupacion: 'Total', col2015: 67.8, col2016: 64.8, col2017: 67.3, col2018: 63.2},
  {agrupacion: 'Hombre', col2015: 66.6, col2016: 65.9, col2017: 66.5, col2018: 63.5},
  {agrupacion: 'Mujer', col2015: 69, col2016: 63.7, col2017: 68.2, col2018: 62.9},
  {agrupacion: 'Caaguazú', col2015: 0, col2016: 0, col2017: 0, col2018: 0},
  {agrupacion: 'Total', col2015: 75, col2016: 74.5, col2017: 76.5, col2018: 73.2},
  {agrupacion: 'Hombre', col2015: 75.3, col2016: 73.4, col2017: 75.4, col2018: 72.6},
  {agrupacion: 'Mujer', col2015: 74.7, col2016: 75.7, col2017: 77.6, col2018: 73.9},
  {agrupacion: 'Caazapá', col2015: 0, col2016: 0, col2017: 0, col2018: 0},
  {agrupacion: 'Total', col2015: 63.6, col2016: 56.6, col2017: 59.8, col2018: 57.1},
  {agrupacion: 'Hombre', col2015: 63.9, col2016: 55.6, col2017: 59.6, col2018: 57.4},
  {agrupacion: 'Mujer', col2015: 63.2, col2016: 57.5, col2017: 59.9, col2018: 56.8},
  {agrupacion: 'Itapúa', col2015: 0, col2016: 0, col2017: 0, col2018: 0},
  {agrupacion: 'Total', col2015: 74.3, col2016: 69.6, col2017: 66.2, col2018: 63.7},
  {agrupacion: 'Hombre', col2015: 73.6, col2016: 69.1, col2017: 67.7, col2018: 65.4},
  {agrupacion: 'Mujer', col2015: 75.2, col2016: 70.1, col2017: 64.7, col2018: 62},
  {agrupacion: 'Misiones', col2015: 0, col2016: 0, col2017: 0, col2018: 0},
  {agrupacion: 'Total', col2015: 79.4, col2016: 74.6, col2017: 79.2, col2018: 72.9},
  {agrupacion: 'Hombre', col2015: 81.3, col2016: 72.7, col2017: 77.9, col2018: 72.9},
  {agrupacion: 'Mujer', col2015: 77.4, col2016: 76.5, col2017: 80.5, col2018: 72.9},
  {agrupacion: 'Paraguarí', col2015: 0, col2016: 0, col2017: 0, col2018: 0},
  {agrupacion: 'Total', col2015: 67.2, col2016: 66, col2017: 63.5, col2018: 62.7},
  {agrupacion: 'Hombre', col2015: 67.7, col2016: 67.2, col2017: 65, col2018: 62.3},
  {agrupacion: 'Mujer', col2015: 66.7, col2016: 64.8, col2017: 62, col2018: 63.1},
  {agrupacion: 'Alto Paraná', col2015: 0, col2016: 0, col2017: 0, col2018: 0},
  {agrupacion: 'Total', col2015: 98.9, col2016: 92.8, col2017: 94.2, col2018: 92.3},
  {agrupacion: 'Hombre', col2015: 97, col2016: 93.4, col2017: 94.3, col2018: 92.5},
  {agrupacion: 'Mujer', col2015: 100.8, col2016: 92.2, col2017: 94.2, col2018: 92.1},
  {agrupacion: 'Central', col2015: 0, col2016: 0, col2017: 0, col2018: 0},
  {agrupacion: 'Total', col2015: 87, col2016: 85.2, col2017: 90.3, col2018: 83.5},
  {agrupacion: 'Hombre', col2015: 86.6, col2016: 84.5, col2017: 90.6, col2018: 83.5},
  {agrupacion: 'Mujer', col2015: 87.5, col2016: 86, col2017: 90, col2018: 83.4},
  {agrupacion: 'Ñeembucú', col2015: 0, col2016: 0, col2017: 0, col2018: 0},
  {agrupacion: 'Total', col2015: 68.4, col2016: 67, col2017: 70.3, col2018: 69.9},
  {agrupacion: 'Hombre', col2015: 63.9, col2016: 69.8, col2017: 69.7, col2018: 65.5},
  {agrupacion: 'Mujer', col2015: 73.1, col2016: 64.1, col2017: 70.9, col2018: 74.5},
  {agrupacion: 'Amambay', col2015: 0, col2016: 0, col2017: 0, col2018: 0},
  {agrupacion: 'Total', col2015: 98, col2016: 100.4, col2017: 100.8, col2018: 95.1},
  {agrupacion: 'Hombre', col2015: 99.8, col2016: 100.5, col2017: 101.9, col2018: 97.5},
  {agrupacion: 'Mujer', col2015: 96.1, col2016: 100.3, col2017: 99.6, col2018: 92.5},
  {agrupacion: 'Canindeyú', col2015: 0, col2016: 0, col2017: 0, col2018: 0},
  {agrupacion: 'Total', col2015: 79.5, col2016: 82.3, col2017: 85, col2018: 82.2},
  {agrupacion: 'Hombre', col2015: 77.6, col2016: 79.9, col2017: 82.2, col2018: 82.1},
  {agrupacion: 'Mujer', col2015: 81.5, col2016: 84.7, col2017: 88, col2018: 82.4},
  {agrupacion: 'Presidente Hayes', col2015: 0, col2016: 0, col2017: 0, col2018: 0},
  {agrupacion: 'Total', col2015: 82, col2016: 81.8, col2017: 87.5, col2018: 78},
  {agrupacion: 'Hombre', col2015: 82.9, col2016: 78.6, col2017: 88.5, col2018: 81.3},
  {agrupacion: 'Mujer', col2015: 81.1, col2016: 85.2, col2017: 86.4, col2018: 74.6},
  {agrupacion: 'Boquerón', col2015: 0, col2016: 0, col2017: 0, col2018: 0},
  {agrupacion: 'Total', col2015: 99.3, col2016: 103.7, col2017: 97.6, col2018: 111.1},
  {agrupacion: 'Hombre', col2015: 98.2, col2016: 107.4, col2017: 100.8, col2018: 112.3},
  {agrupacion: 'Mujer', col2015: 100.4, col2016: 99.8, col2017: 94.3, col2018: 109.8},
  {agrupacion: 'Alto Paraguay', col2015: 0, col2016: 0, col2017: 0, col2018: 0},
  {agrupacion: 'Total', col2015: 83, col2016: 75.2, col2017: 88.7, col2018: 81.6},
  {agrupacion: 'Hombre', col2015: 88, col2016: 77, col2017: 92, col2018: 90.1},
  {agrupacion: 'Mujer', col2015: 77.7, col2016: 73.4, col2017: 85.2, col2018: 72.9}];

datos-graficos-data.php file:

<?php
$q = $_REQUEST['q'];
switch ($q) {
   case 'guardar_grafico':
        $db = DataBase::conectar();
        $id = $_REQUEST['id'];//ID
        $froala = $db->clearText($_REQUEST['meta']);
        $tipo_grafico = $db->clearText($_REQUEST['tipo']);
        $tipo_excel = $db->clearText($_REQUEST['excel']);
        $datos_grafico = $_REQUEST['datos'];//OBJECT ARRAY
        $cabecera_grafico = $_REQUEST['headers'];//ARRAY HEADER EX=[2015,2016,2017,ETC]
        $array = $_REQUEST['array'];//ARRAY OF SELECTED DATA
        $json_encoded_array = json_encode($array); //JSON_ENCODE TO STORE IN DB
        
        //IRRELEVANT CODE HERE

        //UPDATE DATA, THIS IS WHERE THAT ARRAY IS SAVED
        $db->setQuery("UPDATE indicadores SET 
        datos='$froala',
        tipo_excel='$tipo_excel',
        tipo_grafico='$tipo_grafico',
        datos_grafico='$json_encoded_array'
        WHERE id_indicador=$id");
        if(!$db->alter()){
            echo "Error: ". $db->getError();
            $db->rollback();  //REVERT CHANGES                
        exit;
        }

        $db->commit();

        echo json_encode('Data saved successfully');
    break;
}

CodePudding user response:

Please change your question to the following...

"How to convert UNICODE values to UNICODE characters within a string?"

If that is the question, then I think you can build something like this…

Create an object in JavaScript containing all the key/value pairs for the entire Unicode table. Here is that link https://unicode-table.com/en/

All Unicode characters should be preceded by “U ” then four alpha numeric characters after the “U ”. The following code should work for finding multiple Unicodes within a string.

You will have to manually create the entire Unicode table as a JavaScript object or as a .json and import that .json into your application. Either way the following code will work…

const objUnicode = {
    '00F3': 'ó',
    '006E': 'n',
    '00D1': 'Ñ',
    '00FA': 'ú'
}

//let string1 = 'ConcepciU 00F3n'
//convertUnicode(string1)

let string2 = 'U 00D1eembucU 00FA'

convertUnicode(string2)

function convertUnicode(string) {
    let arraySplit = string.split('U ') //split string at U  which excludes the U  from the split
    let arrayUnicode = [] //empty array to hold the unicode values to be used as keys
    let unicode
    //extract all unicodes within the string and add to array
    arraySplit.forEach((element) => {
        if (element === '') {
            return
        }
        unicode = element.slice(0, 4)
        arrayUnicode.push(unicode)
    })
    // loop through all the unicodes in the array and replace orignal string unicode values with string values from objUnicode
    arrayUnicode.forEach((element) => {
        string = string.replaceAll(`U ${element}`, objUnicode[element])
    })

    console.log(string)
}

  • Related