|
|
|
แจกโค้ดตรวจสอบว่าสตริงนั้น base64 encode ถูกต้องหรือไม่ |
|
|
|
|
|
|
|
Code (PHP)
function isValidBase64($string): bool
{
if (!is_string($string)) {
return false;
}
$decoded = base64_decode($string, true);
if (false === $decoded) {
return false;
}
if (false === json_encode($decoded)) {
return false;
}
unset($decoded);
return true;
}// isValidBase64
Code (PHP)
$strings = [
'NTU1NQ==' => true,
'dGVzdA==' => true,
'aGVsbG8=' => true,
'4Liq4Lin4Lix4Liq4LiU4Li1' => true,
'5555' => false,
'test' => false,
'hello' => false,
'สวัสดี' => false,
];
assert_options(ASSERT_BAIL, 1);
foreach ($strings as $string => $assert) {
$decoded = base64_decode($string);
$isValid = isValidBase64($string);
echo '<strong>' . $string . '</strong> decoded: ' . str_replace(['<', '>'], ['<', '>'], (var_export($decoded, true))) . '<br>' . PHP_EOL;
echo ' is valid: ' . ($isValid === true ? '<span style="color: green;">YES</span>' : '<span style="color: red;">NO</span>') . '<br>' . PHP_EOL;
assert($isValid === $assert, 'Failed assert for ' . $string);
}
อ่านเพิ่มเติม https://rundiz.com/?p=753
Tag : PHP
|
|
|
|
|
|
Date :
2021-11-03 15:25:29 |
By :
mr.v |
View :
484 |
Reply :
0 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
Load balance : Server 01
|