asimlqt / encode-decode
PHP的编码/解码库
0.0.2
2017-07-04 11:06 UTC
Requires
- php: >=5.6.0
Requires (Dev)
- phpunit/phpunit: ^5.6
This package is not auto-updated.
Last update: 2024-09-14 20:14:53 UTC
README
这个小库提供了一种简单一致的方式来在PHP中对数据进行编码和解码。要么是因为没有现成的解决方案,要么是为了封装原生调用以提供更简单的接口和错误处理。
目前只有2个编码器
- Base64Url
- Json
Base64Url
编码示例
try {
$encoder = new Base64Url();
$encoded = $encoder->encode("Encode/Decode library for PHP");
} catch (EncodingException $e) {
// handle exception
}
// encoded === "RW5jb2RlL0RlY29kZSBsaWJyYXJ5IGZvciBQSFA"
解码示例
try {
$encoder = new Base64Url();
$decoded = $encoder->decode("RW5jb2RlL0RlY29kZSBsaWJyYXJ5IGZvciBQSFA");
} catch (DecodingException $e) {
// handle exception
}
// decoded === "Encode/Decode library for PHP"
Json
编码示例
try {
$encoder = new Json();
$encoded = $encoder->encode(["encode" => "decode"]);
} catch (EncodingException $e) {}
解码示例
try {
$encoder = new Json();
$decoded = $encoder->decode('{"encode":"decode"}');
} catch (DecodingException $e) {}
默认情况下,解码将返回一个数组,如果您想解码为对象,则将assoc设置为false
$encoder->setAssoc(false);