0

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.

  • When you log your request, what header charset do you send? Have you tried setting the ajax option: `contentType: "application/json;charset=ISO-8859-1"` or `contentType: "application/json;charset=UTF-8` – mhaendler Sep 16 '22 at 11:47
  • Technilly when you send an ajax request without specifying the charset it is set to UTF-8 by default, but I didn't tried charset=ISO-8859-1. – user20011713 Sep 16 '22 at 11:52
  • 1
    Maybe this post may help you [accent ajax encoding issue](https://stackoverflow.com/questions/1904119/accent-ajax-encoding-issue) – Bl457Xor Sep 16 '22 at 11:54
  • Have you checked with localeCompare. `goodAccents.localeCompare(accentsDetached, 'en')` – RioSant Sep 16 '22 at 11:59

1 Answers1

0

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

RioSant
  • 156
  • 9
  • This resolved my issue. Thank you so much. Also I want to add that you can accomplish the same result in PHP using the Normalizer class. For example: `$normalizedString = Normalizer::normalize('á é í ó ú ñ');` – user20011713 Sep 19 '22 at 07:50