recycledbeans / is-base64
提供了一种快速简单的方法来判断字符串是否是base64编码的。
v1.0.6
2019-06-13 15:29 UTC
This package is auto-updated.
Last update: 2024-09-25 02:03:45 UTC
README
有时候,我发现有必要验证一段文本是否已经是base64编码的。由于PHP没有包含一个简单的函数来做这件事,所以我决定写这个。这个包只做了一件事:添加了一些辅助函数,用于判断文本字符串是否是base64编码的。
安装
您可以使用composer轻松安装
composer require recycledbeans/is-base64
用法
is_base64()
<?php $is_b64 = is_base64('I am not base64-encoded'); // returns false $is_b64 = is_base64('SSBhbSBiYXNlNjQtZW5jb2RlZAo='); // returns true
is_base64_encode()
当您不想在编码前检查是否已编码时非常有用。将确保字符串是base64编码的,如果字符串之前未编码,则进行编码。
$my_encoded_string = is_base64_encode($_REQUEST['could_be_base64_encoded_or_not']); // returns base64-encoded string
is_base64_decode()
当您不想在解码前检查是否已编码时非常有用。无论原始字符串是否已经是base64编码的,都将返回字符串值。
$my_encoded_string = is_base64_decode($_REQUEST['could_be_base64_encoded_or_not']); // returns decoded string
示例
$my_encoded_string = $_REQUEST['should_be_encoded']; if (! is_base64($my_encoded_string)) { $my_encoded_string = base64_encode($my_encoded_string); }