Skip to content

Instantly share code, notes, and snippets.

@gabrielbarros
Created March 8, 2024 14:10
Show Gist options
  • Save gabrielbarros/8a21b98790c4b45661221affa051ea02 to your computer and use it in GitHub Desktop.
Save gabrielbarros/8a21b98790c4b45661221affa051ea02 to your computer and use it in GitHub Desktop.
Convert from ISO-8859-1 to UTF-8 and vice-versa; test if it's valid UTF-8
<?php
// ISO-8859-1 → UTF-8
function latin1ToUtf8(string $str): string
{
return iconv('ISO-8859-1', 'UTF-8', $str);
}
// UTF-8 → ISO-8859-1
function utf8ToLatin1(string $str): string
{
return iconv('UTF-8', 'ISO-8859-1', $str);
}
function isValidUtf8(string $str): bool
{
return mb_check_encoding($str, 'UTF-8');
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment