Table of contents
2. Array and object conversion
3. URL formatting and anti-formatting
4. Timestamp and date format conversion
5. Array serialization and deserialization
6. String base64_encode encoding and base64_decode decoding
7. HTML transcoding and decoding
8. Array and string conversion
1. Array to json conversion
$arr=['a'=>1,'b'=>2];
$json=json_encode($arr);
echo '数组转json:';
echo "\r\n".$json;
$arr2=json_decode($json,true);
echo "\r\njson转数组:\r\n";
var_dump($arr2);
2. Array and object conversion
$array = array('name' => 'Tom', 'age' => 20);
$obj = (object)$array;
echo "数组转对象:\r\n";
var_dump($obj);
$param = json_encode($obj);
$param = json_decode($param, true);
echo "对象转数组:\r\n";
var_dump($param);
exit;
3. URL formatting and anti-formatting
$url='xxx.com?a=12';
$urlCode=urlencode($url);
echo "url格式化:\r\n";
echo $urlCode;
$url2=urldecode($url);
echo "\r\nurl反格式化:\r\n";
echo $url2;
4. Timestamp and date format conversion
$time=time();
$date=date("Y-m-d H:i:s",$time);
echo "时间戳转日期格式:\r\n";
echo $date."\r\n";
$time2=strtotime($date);
echo "日期格式转时间戳:\r\n";
echo $time2."\r\n";
5. Array serialization and deserialization
$obj=['a'=>1,'b'=>2];
$serializedObj = serialize($obj);;
echo "数组序列化:\r\n";
echo $serializedObj."\r\n";
$obj = unserialize($serializedObj);
echo "反序列化:\r\n";
var_dump($obj);
6. String base64_encode encoding and base64_decode decoding
$data='123abc';
$base64String = base64_encode($data);
echo "base64_encode编码:\r\n";
echo $base64String."\r\n";
$data2 = base64_decode($base64String);
echo "base64_decode解码:\r\n";
echo $data2;
7. HTML transcoding and decoding
$str = "<p>This is a <strong>bold</strong> statement.</p>";
$encodedStr = htmlspecialchars($str);
echo "html加码:\r\n";
echo $encodedStr;
$decodedStr = html_entity_decode($encodedStr);
echo "\r\nhtml解码:\r\n";
echo $decodedStr;
8. Array and string conversion
$data = ['a'=>1,'b'=>2];
$str = implode($data,',');
echo "连接数组:\r\n";
echo $str;
$arr =explode(',',$str);
echo "\r\nhtml分割数组:\r\n";
var_dump($arr);
9. String case conversion
$str='ab!2A';
$str1 = strtolower($str);
echo "转小写:\r\n";
echo $str1;
$str2 =strtoupper($str1);
echo "\r\n转大写:\r\n";
echo $str2;
10. Data type conversion
$str='1.23';
$str1 = (float) $str;
echo "转浮点型:\r\n";
var_dump($str1) ;
$str2 = (string) $str1;
echo "\r\n转字符串:\r\n";
var_dump($str2) ;
(int): Convert the value to an integer type. (float) or (double): Convert the value to a floating point type. (string): Convert the value to string type. (array): Converts the value to an array type. (object): converts the value to an object type