PHP 常用函数总结(三)

7、PHP JSON 格式

json_encode ( mixed $value [, int $options = 0 [, int $depth = 512 ]] )

返回字符串,包含了 value 值 JSON 形式的表示。

编码受传入的 options 参数影响,此外浮点值的编码依赖于 serialize_precision

参数

value

待编码的 value ,除了resource 类型之外,可以为任何数据类型。

所有字符串数据的编码必须是 UTF-8。

options

由以下常量组成的二进制掩码: JSON_HEX_QUOT, JSON_HEX_TAG, JSON_HEX_AMP, JSON_HEX_APOS, JSON_NUMERIC_CHECK,JSON_PRETTY_PRINT, JSON_UNESCAPED_SLASHES, JSON_FORCE_OBJECT, JSON_PRESERVE_ZERO_FRACTION, JSON_UNESCAPED_UNICODE,JSON_PARTIAL_OUTPUT_ON_ERROR。 关于 JSON 常量详情参考JSON 常量

以下常量表示了 json_last_error() 所返回的错误类型。

JSON_ERROR_NONE (integer)
没有错误发生。自 PHP 5.3.0 起生效。
JSON_ERROR_DEPTH (integer)
到达了最大堆栈深度。自 PHP 5.3.0 起生效。
JSON_ERROR_STATE_MISMATCH (integer)
出现了下溢(underflow)或者模式不匹配。自 PHP 5.3.0 起生效。
JSON_ERROR_CTRL_CHAR (integer)
控制字符错误,可能是编码不对。自 PHP 5.3.0 起生效。
JSON_ERROR_SYNTAX (integer)
语法错误。 自 PHP 5.3.0 起生效。
JSON_ERROR_UTF8 (integer)
异常的 UTF-8 字符,也许是因为不正确的编码。 自 PHP 5.3.3 起生效。
JSON_ERROR_RECURSION (integer)
The object or array passed to json_encode() include recursive references and cannot be encoded. If theJSON_PARTIAL_OUTPUT_ON_ERROR option was given, NULL will be encoded in the place of the recursive reference. Available since PHP 5.5.0.
JSON_ERROR_INF_OR_NAN (integer)
The value passed to json_encode() includes either NAN or INF. If the JSON_PARTIAL_OUTPUT_ON_ERROR option was given, 0 will be encoded in the place of these special numbers. Available since PHP 5.5.0.
JSON_ERROR_UNSUPPORTED_TYPE (integer)
A value of an unsupported type was given to json_encode(), such as a resource. If the JSON_PARTIAL_OUTPUT_ON_ERROR option was given, NULL will be encoded in the place of the unsupported value. Available since PHP 5.5.0.
JSON_ERROR_INVALID_PROPERTY_NAME (integer)
A key starting with u0000 character was in the string passed to json_decode() when decoding a JSON object into a PHP object. Available since PHP 7.0.0.
JSON_ERROR_UTF16 (integer)
Single unpaired UTF-16 surrogate in unicode escape contained in the JSON string passed to json_encode(). Available since PHP 7.0.0.
下面的常量可以和 json_decode() 的 form 选项结合使用。

JSON_BIGINT_AS_STRING (integer)
将大数字编码成原始字符原来的值。 自 PHP 5.4.0 起生效。
JSON_OBJECT_AS_ARRAY (integer)
Decodes JSON objects as PHP array. This option can be added automatically by calling json_decode() with the second parameter equal to TRUE. Available since PHP 5.4.0.
下面的常量可以和 json_encode() 的 form 选项结合使用。

JSON_HEX_TAG (integer)
所有的 < 和 > 转换成 u003C 和 u003E。 自 PHP 5.3.0 起生效。
JSON_HEX_AMP (integer)
所有的 & 转换成 u0026。 自 PHP 5.3.0 起生效。
JSON_HEX_APOS (integer)
所有的 ' 转换成 u0027。 自 PHP 5.3.0 起生效。
JSON_HEX_QUOT (integer)
所有的 " 转换成 u0022。 自 PHP 5.3.0 起生效。
JSON_FORCE_OBJECT (integer)
使一个非关联数组输出一个类(Object)而非数组。 在数组为空而接受者需要一个类(Object)的时候尤其有用。 自 PHP 5.3.0 起生效。
JSON_NUMERIC_CHECK (integer)
将所有数字字符串编码成数字(numbers)。 自 PHP 5.3.3 起生效。
JSON_PRETTY_PRINT (integer)
用空白字符格式化返回的数据。 自 PHP 5.4.0 起生效。
JSON_UNESCAPED_SLASHES (integer)
不要编码 /。 自 PHP 5.4.0 起生效。
JSON_UNESCAPED_UNICODE (integer)
以字面编码多字节 Unicode 字符(默认是编码成 uXXXX)。 自 PHP 5.4.0 起生效。
JSON_PARTIAL_OUTPUT_ON_ERROR (integer)
Substitute some unencodable values instead of failing. Available since PHP 5.5.0.
JSON_PRESERVE_ZERO_FRACTION (integer)
Ensures that float values are always encoded as a float value. Available since PHP 5.6.6.
JSON_UNESCAPED_LINE_TERMINATORS (integer)
The line terminators are kept unescaped when JSON_UNESCAPED_UNICODE is supplied. It uses the same behaviour as it was before PHP 7.1 without this constant. Available since PHP 7.1.0.

depth

设置最大深度。 必须大于0。

返回值

成功则返回 JSON 编码的 string 或者在失败时返回 FALSE 。

json_decode ( string $json [, bool $assoc = false [, int $depth = 512 [, int $options = 0 ]]] )

接受一个 JSON 编码的字符串并且把它转换为 PHP 变量。

参数

json

待解码的 json string 格式的字符串。

这个函数仅能处理 UTF-8 编码的数据。

assoc

当该参数为 TRUE 时,将返回 array 而非 object 。

depth

指定递归深度。

options

JSON解码的掩码选项。 现在有两个支持的选项。 第一个是JSON_BIGINT_AS_STRING, 用于将大整数转为字符串而非默认的float类型。第二个是 JSON_OBJECT_AS_ARRAY, 与将assoc设置为 TRUE 有相同的效果。

#EXAMPLE

<?php
$json = '{"a":1,"b":2,"c":3,"d":4,"e":5}';

$json_de_obj = json_decode($json);
$json_de_array = json_decode($json, true);
var_dump($json_de_obj);
var_dump($json_de_array);

$json_en_obj = json_encode($json_de_obj);
$json_en_array = json_encode($json_de_array);
var_dump($json_en_obj );
var_dump($json_en_array );

?>

输出结果:
object(stdClass)[10]
  public 'a' => int 1
  public 'b' => int 2
  public 'c' => int 3
  public 'd' => int 4
  public 'e' => int 5

array (size=5)
  'a' => int 1
  'b' => int 2
  'c' => int 3
  'd' => int 4
  'e' => int 5
string '{"a":1,"b":2,"c":3,"d":4,"e":5}' (length=31)
string '{"a":1,"b":2,"c":3,"d":4,"e":5}' (length=31)

8、PHP 加密解密函数

(mcrypt7.1开始扩展被废弃,OpenSSL扩展代替)

加密函数:

单向加密函数:

md5(string,raw);

参数描述
string 必需。规定要计算的字符串。
raw

可选。规定十六进制或二进制输出格式:

  • TRUE - 原始 16 字符二进制格式
  • FALSE - 默认。32 字符十六进制数

crypt(string, salt) 

返回使用 DES、Blowfish 或 MD5 算法加密的字符串。

在不同的操作系统上,该函数的行为不同,某些操作系统支持一种以上的算法类型。在安装时,PHP 会检查什么算法可用以及使用什么算法。

具体的算法依赖于 salt 参数的格式和长度。通过增加由使用特定加密方法的特定字符串所生成的字符串数量,salt 可以使加密更安全。

这里有一些和 crypt() 函数一起使用的常量。这些常量值是在安装时由 PHP 设置的。

常量:

[CRYPT_SALT_LENGTH] 默认的加密长度。使用标准的 DES 加密,长度为 2
[CRYPT_STD_DES] 基于标准 DES 算法的散列使用 "./0-9A-Za-z" 字符中的两个字符作为盐值。在盐值中使用非法的字符将导致 crypt() 失败。
[CRYPT_EXT_DES]

扩展的基于 DES 算法的散列。其盐值为 9 个字符的字符串,由 1 个下划线后面跟着 4 字节循环次数和 4 字节盐值组成。它们被编码成可打印字符,每个字符 6 位,有效位最少的优先。0 到 63 被编码为 "./0-9A-Za-z"。在盐值中使用非法的字符将导致 crypt() 失败。

[CRYPT_MD5] MD5 散列使用一个以 $1$ 开始的 12 字符的字符串盐值。
[CRYPT_BLOWFISH] Blowfish 算法使用如下盐值:“$2a$”,一个两位 cost 参数,“$” 以及 64 位由 “./0-9A-Za-z” 中的字符组合而成的字符串。在盐值中使用此范围之外的字符将导致 crypt() 返回一个空字符串。两位 cost 参数是循环次数以 2 为底的对数,它的范围是 04-31,超出这个范围将导致 crypt() 失败。
CRYPT_SHA256 SHA-256 算法使用一个以 $5$ 开头的 16 字符字符串盐值进行散列。如果盐值字符串以 “rounds=<N>$” 开头,N 的数字值将被用来指定散列循环的执行次数,这点很像 Blowfish 算法的 cost 参数。默认的循环次数是 5000,最小是 1000,最大是 999,999,999。超出这个范围的 N 将会被转换为最接近的值。
CRYPT_SHA512 SHA-512 算法使用一个以 $6$ 开头的 16 字符字符串盐值进行散列。如果盐值字符串以 “rounds=<N>$” 开头,N 的数字值将被用来指定散列循环的执行次数,这点很像 Blowfish 算法的 cost 参数。默认的循环次数是 5000,最小是 1000,最大是 999,999,999。超出这个范围的 N 将会被转换为最接近的值。

在该函数支持多种算法的系统上,如果支持上述常量则设置为 "1",否则设置为 "0"。

password_hash(string $password , int $algo [, array $options ])  是crypt()的一个简单封装,并且完全与现有的密码哈希兼容.推荐使用该函数来进行单向加密。

盐值(salt)选项从 PHP 7.0.0 开始被废弃(deprecated)了。 现在最好选择简单的使用默认产生的盐值。

当前支持的算法:

  • PASSWORD_DEFAULT - 使用 bcrypt 算法 (PHP 5.5.0 默认)。 注意,该常量会随着 PHP 加入更新更高强度的算法而改变。 所以,使用此常量生成结果的长度将在未来有变化。 因此,数据库里储存结果的列可超过60个字符(最好是255个字符)。
  • PASSWORD_BCRYPT - 使用 CRYPT_BLOWFISH 算法创建散列。 这会产生兼容使用 "$2y$" 的 crypt()。 结果将会是 60 个字符的字符串, 或者在失败时返回 FALSE
  • PASSWORD_ARGON2I - 使用 Argon2 散列算法创建散列。

PASSWORD_BCRYPT 支持的选项:

  • salt(string) - 手动提供散列密码的盐值(salt)。这将避免自动生成盐值(salt)。

    省略此值后,password_hash() 会为每个密码散列自动生成随机的盐值。这种操作是有意的模式。

    Warning

    盐值(salt)选项从 PHP 7.0.0 开始被废弃(deprecated)了。 现在最好选择简单的使用默认产生的盐值。

  • cost (integer) - 代表算法使用的 cost。crypt() 页面上有 cost 值的例子。

    省略时,默认值是 10。 这个 cost 是个不错的底线,但也许可以根据自己硬件的情况,加大这个值。

PASSWORD_ARGON2I 支持的选项:

  • memory_cost (integer) - 计算 Argon2 散列时的最大内存(单位:字节 byte)。默认值: PASSWORD_ARGON2_DEFAULT_MEMORY_COST

  • time_cost (integer) - 计算 Argon2 散列时最多的时间。默认值: PASSWORD_ARGON2_DEFAULT_TIME_COST

  • threads (integer) - 计算 Argon2 散列时最多的线程数。默认值: PASSWORD_ARGON2_DEFAULT_THREADS

可解密的加密函数:

mcrypt_encrypt(string $cipher , string $key , string $data , string $mode [, string $iv ] )  

参数

cipher

MCRYPT_ciphername 常量中的一个,或者是字符串值的算法名称。

key

加密密钥。 如果密钥长度不是该算法所能够支持的有效长度,则函数将会发出警告并返回 FALSE

data

使用给定的 cipher 和 mode 加密的数据。 如果数据长度不是 n*分组大小,则在其后使用 '' 补齐。

返回的密文长度可能比 data 更大。

mode

MCRYPT_MODE_modename 常量中的一个,或以下字符串中的一个:"ecb","cbc","cfb","ofb","nofb" 和 "stream"。

iv

Used for the initialization in CBC, CFB, OFB modes, and in some algorithms in STREAM mode. If the provided IV size is not supported by the chaining mode or no IV was provided, but the chaining mode requires one, the function will emit a warning and return FALSE.

PHP5.6开始不再接受无效长度的 key and iv 参数。 如果参数长度无效,则 mcrypt_decrypt() 函数会产生警告并且返回 FALSE。 之前版本中,对于长度不足的密钥和初始向量会在其后补齐 '' 使其达到有效长度。

openssl_encrypt(string $data , string $method , string $key [, int $options = 0 [, string $iv = "" [, string&$tag = NULL [, string $aad = "" [, int $tag_length = 16 ]]]]] )

以指定的方式和 key 加密数据,返回原始或 base64 编码后的字符串。

参数

data

待加密的明文信息数据。

method

密码学方式。openssl_get_cipher_methods() 可获取有效密码方式列表。

key

key。

options

options 是以下标记的按位或: OPENSSL_RAW_DATA 、 OPENSSL_ZERO_PADDING

iv

非 NULL 的初始化向量。

tag

使用 AEAD 密码模式(GCM 或 CCM)时传引用的验证标签。

aad

附加的验证数据。

tag_length

验证 tag 的长度。GCM 模式时,它的范围是 4 到 16。

返回值

成功时返回加密后的字符串, 或者在失败时返回 FALSE

错误/异常

method 传入未知算法时,产生 E_WARNING 级别的错误。

iv 传入空字符串时产生 E_WARNING 级别的错误。

解密函数:

mcrypt_decrypt()

openssl_decrypt()

编码转换

base64_encode(string)        base64编码

base64_decode(string)        base64解码     

urlencode(string)        URL编码    

urldecode(string)        URL解码

convert_uuencode()        使用 uuencode 算法对字符串进行编码。把所有字符串(包括二进制)编码为可打印的字符,以确保其数据库存储及网络传输数据的安全。【uuencoded 数据比原数据大约增大 35%。】

convert_uudecode()        对 uuencode 编码的字符串进行解码。

htmlentities()        把字符转换为 HTML 实体。

html_entity_decode()         把 HTML 实体转换为字符。

htmlspecialchars()         把预定义的字符转换为 HTML 实体。

htmlspecialchars_decode()         把预定义的 HTML 实体转换为字符。

预定义的字符是:

  • & (和号)成为 &
  • " (双引号)成为 "
  • ' (单引号)成为 '
  • < (小于)成为 <
  • > (大于)成为 >

 #EXAMPLE1

<?php
    # 密钥应该是随机的二进制数据,
    # 开始使用 scrypt, bcrypt 或 PBKDF2 将一个字符串转换成一个密钥
    # 密钥是 16 进制字符串格式

 $str = "需要加密的字符串";  
 $key = pack('H*', cbc04b7e103a0cd8b54763051cef08bc55abe029fdebae5e1d417e2ffb2a00a3"); 


 # 显示 AES-128, 192, 256 对应的密钥长度:   
 # 16,24,32 字节。

 $cipher = MCRYPT_RIJNDAEL_128;  
 $mode = MCRYPT_MODE_CBC;  
 $iv = mcrypt_create_iv(mcrypt_get_iv_size($cipher,$mode),MCRYPT_RAND);  
   
 $str_encrypt = mcrypt_encrypt($cipher,$key,$str,$mode,$iv);
 $str_encrypt  = base64_encode($str_encrypt);
 echo "加密后的内容是:".$str_encrypt."<br>";  
  
 $str_decrypt = base64_decode($str_decrypt);
 $str_decrypt = mcrypt_decrypt($cipher,$key,$str_encrypt,$mode,$iv);  
 echo "解密后的内容:".$str_decrypt."<br>";  

 #EXAMPLE2

<?php
//$key should have been previously generated in a cryptographically safe way, like openssl_random_pseudo_bytes
$plaintext = "message to be encrypted";
$cipher = "aes-128-gcm";
if (in_array($cipher, openssl_get_cipher_methods()))
{
    $ivlen = openssl_cipher_iv_length($cipher);
    $iv = openssl_random_pseudo_bytes($ivlen);
    $ciphertext = openssl_encrypt($plaintext, $cipher, $key, $options=0, $iv, $tag);
    //store $cipher, $iv, and $tag for decryption later
    $original_plaintext = openssl_decrypt($ciphertext, $cipher, $key, $options=0, $iv, $tag);
    echo $original_plaintext."
";
}
?>
原文地址:https://www.cnblogs.com/zwesy/p/9330029.html