php讲转义符号与json文件的趣事情
2024-10-19 06:28:28
php中屡试不爽的数组和json
- json_encode与json_decode
- urlencode与urldecode
- addslashes与stripslashes
- addcslashes与stripcslashes
- 以上这些请自行了解
附上两组操作实例
实例一,借助urlencode函数
function arrayRecursive(&$array, $function, $apply_to_keys_also = false)
{
static $recursive_counter = 0;
if (++$recursive_counter > 1000) {
die('possible deep recursion attack');
}
foreach ($array as $key => $value) {
if (is_array($value)) {
arrayRecursive($array[$key], $function, $apply_to_keys_also);
} else {
$array[$key] = $function($value);
} if ($apply_to_keys_also && is_string($key)) {
$new_key = $function($key);
if ($new_key != $key) {
$array[$new_key] = $array[$key];
unset($array[$key]);
}
}
}
$recursive_counter--;
} function my_json($array) {
arrayRecursive($array, 'urlencode', true);
$json = json_encode($array);
return urldecode($json);
}
$arr = [
'a' => 123,
'b' => 456,
'http://www.jjxhp.com/abc/sasa.php' => 'tstst你好',
'http://www.baidu.com/' => '我们都是好人吧',
'rew你好' => 'dsdsqwq'
]; $str = my_json($arr); file_put_contents(__DIR__.'/json_str.txt', $str);实例二,借助 json_encode与stripslashes
$new_str = json_encode($arr, JSON_UNESCAPED_UNICODE);
file_put_contents(__DIR__.'/new_json_str.txt', $new_str);
$new_str1 = stripslashes($new_str);
file_put_contents(__DIR__.'/new_json_str1.txt', $new_str1);
附言
- htmlentities与htmlspecialchars也脑补吧,自己感觉真的很好用,嘻嘻!!
最新文章
- 【C++沉思录】句柄2
- PE文件结构部分解析以及输入的定位
- PDA手持终端在ERP系统仓库管理出入库盘点环节的应用
- hdoj 1231 最大连续子序列
- 在使用Kettle的集群排序中 Carte的设定——(基于Windows)
- Assumptions
- Tomcat配置一个ip绑定多个域名
- Oracle中的单引号问题
- all about AIX MPIO
- JMS分布式应用程序异步消息解决方案EhCache 高速缓存同步问题
- 手机端仿ios的省市县3级联动脚本一
- Elasticsearch系列(5):深入搜索
- MYSQL数据库常用操作命令
- AT2412 最大の和
- MobX基础 ----- 类的静态属性和装饰器
- Synchronized和java.util.concurrent.locks.Lockde区别联系
- Class__Two
- Sprint最后一天
- linux修改文件所有者和文件所在组
- DPDK报文分类与访问控制