0

I am experiencing some problem on UTF-8 Encoding. I have a CSV file and this is the content of it:

Quién tú - Tes,  más , S03
Who you, More, SO2

I have extracted it one by one and I have this condition in my loop

if(mb_detect_encoding($exploded_value[$i], 'UTF-8', true))
{
   echo $cleaned_data = utf8_encode($exploded_value[$i]);
}
else
{
  echo $cleaned_data=$exploded_value[$i];
}

My cleaned data became like this:

Quién tú - Tes
más
S03

Who you
More
SO2

Character's like á, ç, ú gets decoded and when retrieved it gives the wrong output.

Text with Spanish character are being detected as UTF-8 encode character. So, it falls into utf8_encode($exploded_value[$i]). And when utf8_encode perform its process it got decoded.

Declaration of my meta content type is charset=UTF8

Anyone have encountered this issue. Can you share on how did you fix it? Please help. I have googled around and didn't find any luck.

1 Answer 1

1

Your logic is reversed: you are re-encoding what is already utf-8 encoded, and what is not you pass on as-is.

To fix this just switch the if and else bodies:

if(mb_detect_encoding($exploded_value[$i], 'UTF-8', true))
{
   // already UTF-8
   echo $cleaned_data=$exploded_value[$i];
}
else
{
   // not yet UTF-8
   echo $cleaned_data = utf8_encode($exploded_value[$i]);
}
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.