Search code examples
javascriptphpspecial-characterssuitecrm

Accent detached from letter after jquery ajax request


I'm working with a SuiteCRM and at some point I need to send with Jquery ajax in js a string to a php entryPoint where I receive said string. The thing is that when this string contains accents they are separated from the letter but visibly it is still attached to the letter, but they are two separated characters. I'm pasting two string of vowels with accents next. Technically if you copy paste the first string anywhere and start removing characters with the return key it will work just fine, but with the second string if you do the same you will see that when you start deleting characters first it removes the accent from the vowel and then you can remove the vowel itself.

The strings are those:

  1. á é í ó ú ñ
  2. á é í ó ú ñ

So, the first is a console.log from JS which was written by the user and the second is the string I'm receiving in PHP. This is what my JS looks like:

const string = 'á é í ó ú ñ' // The first string listed above
const data = {
   text: string
}

// Send data with ajax to a PHP script
$.ajax({
   method: "POST",
   url: "index.php?entryPoint=tags",
   data: data
}).done(function (response) {
   console.log(response)
})

And this is what my PHP looks like

<?php
$text = $_POST['text'];
$GLOBALS['log']->fatal($text); // This will log "á é í ó ú ñ" which is the second string with the accents detached from the vowels.

The fact is that istead of loggin the string I need to save it in a database to compare it later with other string. Then if the user writes the first string when I compare it with the one I have in the database they will not be the same:

const goodAccents = 'á é í ó ú ñ'
const accentsDetached = 'á é í ó ú ñ' // Received from database 

console.log(goodAccents == accentsDetached) // Logs false

Also I've tried logging the character at the second position of the string where the accents are separated and this is what I'm getting:

console.log('á'.charCodeAt(1)) // Logs 769 which is the unicode number for ´ ---> https://www.fileformat.info/info/unicode/char/0301/index.htm

I don't know if I've explained well myself, but if something can help me I'll appreciate it.

Thanks for your time.


Solution

  • You can normalize your accent string and then compare.See the solution below

    const goodAccents = 'á é í ó ú ñ';
    const accentsDetached = 'á é í ó ú ñ';
    
    console.log(goodAccents.normalize() == accentsDetached.normalize())

    For more you can refer this