Speed up development with full-stack environments for every branch.

Learn More

How to check string encoding [mbstring and PHP]

221 Runs 1269 Views 481 Copies
Saved

Saved

sundip 66

sundip
published 4 years ago

<?php

/*
 * PHP mbstring - How to check string encoding
 */

echo mb_internal_encoding();

$string = "\x00\x81";
$encoding = "Shift_JIS";
if (mb_check_encoding($string, $encoding))
    echo 'valid ' . $encoding . ' encoded byte stream!<br />';
else
    echo 'invalid ' . $encoding . ' encoded byte stream!<br />';

$string = "test string";
$encoding = "UTF-8";
if (mb_check_encoding($string, $encoding))
    echo 'valid ' . $encoding . ' encoded byte stream!<br />';
else
    echo 'invalid ' . $encoding . ' encoded byte stream!<br />';


$string = "valid string";
$encoding = "ISO-8859-1";
if (mb_check_encoding($string, $encoding))
    echo 'valid ' . $encoding . ' encoded byte stream!<br />';
else
    echo 'invalid ' . $encoding . ' encoded byte stream!<br />';

?>
Please login/signup to get access to the terminal.

Your session has timed out.

Dismiss (the page may not function properly).