0

I am trying to build a function in php that will take the input of a string and encode it correctly before displaying it. So far I have:

public function encode($str)
{
  if(!preg_match('!!u', $str)) // Needs to be encoded
  {  
    $str = "YES: " . $str;
    $str = utf8_encode($str);        
  } 
  else 
  {
    $str = "NO: " . $str;
  }
  return $str;
}

The if statement I am using i found here. And it works in some cases but in others it is encoding the string when they shouldn't be.

Perhaps there is an easier way to handle the encoding ?

Community
  • 1
  • 1
William Delong
  • 113
  • 1
  • 8
  • What is your situation, what are you encoding for what purpose, where is the data coming from? Pointing you to the default question & answer on the topic, hold on – Pekka Sep 19 '13 at 01:57
  • possible duplicate of [UTF-8 all the way through](http://stackoverflow.com/questions/279170/utf-8-all-the-way-through) – Pekka Sep 19 '13 at 01:57
  • How does [mb_detect_encoding](http://php.net/manual/en/function.mb-detect-encoding.php) work for you? – Dave Chen Sep 19 '13 at 01:58

0 Answers0