How to check if a string is base64 valid in PHP(如何检查字符串在 PHP 中是否为 base64 有效)
问题描述
我有一个字符串,想用 PHP 测试它是否是有效的 base64 编码.
I have a string and want to test using PHP if it's a valid base64 encoded or not.
推荐答案
我意识到这是一个老话题了,但是使用 strict 参数不一定有帮助.
I realise that this is an old topic, but using the strict parameter isn't necessarily going to help.
对诸如我不是 base 64 编码"之类的字符串运行 base64_decode 不会返回 false.
Running base64_decode on a string such as "I am not base 64 encoded" will not return false.
如果您尝试使用 strict 解码字符串并使用 base64_encode 重新编码,您可以将结果与原始数据进行比较以确定它是否是有效的 bas64 编码值:
If however you try decoding the string with strict and re-encode it with base64_encode, you can compare the result with the original data to determine if it's a valid bas64 encoded value:
if ( base64_encode(base64_decode($data, true)) === $data){
echo '$data is valid';
} else {
echo '$data is NOT valid';
}
这篇关于如何检查字符串在 PHP 中是否为 base64 有效的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持编程学习网!
本文标题为:如何检查字符串在 PHP 中是否为 base64 有效
基础教程推荐
- 学说 dbal querybuilder 作为准备好的语句 2022-01-01
- 有什么方法可以用编码 UTF-8 而不是 Unicode 返回 PHP`json_encode`? 2021-01-01
- PHP PDO MySQL 查询 LIKE ->多个关键词 2021-01-01
- 在PHP中根据W3C规范Unicode 2022-01-01
- Cron Jobs 调用带有变量的 PHP 脚本 2022-01-01
- 如何在 Laravel 中使用 React Router? 2022-01-01
- 如何在 Laravel 5.3 注册中添加动态下拉列表列? 2021-01-01
- 如何替换eregi() 2022-01-01
- PHP 类:全局变量作为类中的属性 2021-01-01
- YouTube API v3 点赞视频,但计数器不增加 2022-01-01
