Skip to content

Instantly share code, notes, and snippets.

@devtronic
Created November 25, 2017 05:58
Show Gist options
  • Select an option

  • Save devtronic/8bc2a015127f0e24e443b059a3494149 to your computer and use it in GitHub Desktop.

Select an option

Save devtronic/8bc2a015127f0e24e443b059a3494149 to your computer and use it in GitHub Desktop.
This functions check before encoding / decoding if the string is already encoded / decoded.
<?php
/**
* utf8_encode only once
*
* @param string $data The data
* @return string The converted string
*/
function doUtf8($data)
{
return !preg_match('!!u', $data) ? utf8_encode($data) : $data;
}
/**
* utf8_decode only once
*
* @param string $data The data
* @return string The converted string
*/
function undoUtf8($data)
{
return preg_match('!!u', $data) ? utf8_decode($data) : $data;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment