php读写json文件
2011-11-15 10:02
302 查看
简介:这是php读写json文件的详细页面,介绍了和php,有关的知识、技巧、经验,和一些php源码等。
class='pingjiaF' frameborder='0' src='http://biancheng.dnbcw.info/pingjia.php?id=333686' scrolling='no'>
A few days ago i had to build a simple comment form. First i
thought about MYSQL etc, but this all seems to be too complicated. So i
came up with a simple solution based on jSon and a TXT file.
So that’s how it cooks:
1. Load the text file with the comments and convert it to an array with json_decode
2. Check if a new comment was posted and save to file when valid.
3. Then we can loop the comment list and create HTML for the output
4. Then we add the HTML form and the list with comments
I know simple but i thought to share it, you can
download
the full example including sorting and some more validation stuff, or check out the example page
.
“php读写json文件”的更多相关文章 》
爱J2EE关注Java迈克尔杰克逊视频站JSON在线工具
http://biancheng.dnbcw.info/php/333686.html pageNo:11
class='pingjiaF' frameborder='0' src='http://biancheng.dnbcw.info/pingjia.php?id=333686' scrolling='no'>
PHP Simple Comments Read/Write jSon data to text file
A few days ago i had to build a simple comment form. First ithought about MYSQL etc, but this all seems to be too complicated. So i
came up with a simple solution based on jSon and a TXT file.
So that’s how it cooks:
1. Load the text file with the comments and convert it to an array with json_decode
1 2 3 4 5 | /* get comments from file */ $commentsText = file_get_contents ( 'comments.txt' ) ; /* create array list from comments */ $commentsList = json_decode ( $commentsText , true ) ; |
1 2 3 4 56 | /* check if new comment is posted and minimum 3 characters are set */ if ( ! empty ( $_POST [ 'comment' ] ) && strlen ( $sComment ) > 3 ) { /* get posted comment and remove all HTML */ $sComment = strip_tags ( $_POST [ 'comment' ] ) ; /* add comment, client IP and date to array */ $commentsList [ 'comments' ] [ ] = array ( 'text' => $sComment , 'ip' => $_SERVER [ 'REMOTE_ADDR' ] , 'date' => time ( ) ) ; /* convert comments to string */ $commentsText = json_encode ( $commentsList ) ; /* save comment to file */ file_put_contents ( $commentsFile , $commentsText ) ; } |
1 2 3 4 56 | /* create html list */ $commentsHTML = "<ul>" ; /* loop all comments */ foreach ( $commentsList [ 'comments' ] as $commentItem ) { // add comment to html list $commentsHTML .= "<li>" . $commentItem [ 'text' ] . "</li>" ; } /* close html comments list */ $commentsHTML .= "</ul>" ; |
1 2 3 4 56 | <form id = "comments" method = "POST" ><form id = "comments" method = "POST" > <h1 > Comments?</ h2 > <div ><?= $errorMessage?></ div > <textarea id = "comment" name = "comment" cols = "70" > </ textarea ><br / > <input type = "submit" value = "yes" / > <?= $commentsHTML?> </ form > |
download
the full example including sorting and some more validation stuff, or check out the example page
.
“php读写json文件”的更多相关文章 》
爱J2EE关注Java迈克尔杰克逊视频站JSON在线工具
http://biancheng.dnbcw.info/php/333686.html pageNo:11
相关文章推荐
- php读写json文件
- php 读写json文件及修改json的方法
- php 读写json文件。追加,修改json
- PHP读写JSON文件以及jQuery的getJSON函数用法
- Unity下关于C#的文件读写三(Json格式读写-基于LitJson简单认识)
- C++通过jsoncpp类库读写JSON文件
- 第二十三课:PHP中文件读写操作
- PHP文件读写操作相关函数总结
- php入门学习知识点六 PHP文件的读写操作代码
- 【转载】PHP如何读写文件
- Unity3d Json 读写文件
- PHP读写文件的两个方法(CodeIgniter框架)
- 用PHP读写NTFS文件系统下的文件摘要信息
- PHP如何读写文件
- php使用多个进程同时控制文件读写示例
- 解析用PHP读写音频文件信息的详解(支持WMA和MP3)
- Python读写json文件中文编码问题
- [PHP 作为iOS后台Json格式HTTP通信及文件上传的实现]
- PHP5.2 写JSON文件时保持中文不变为unicode编码
- 解析用PHP读写音频文件信息的详解(支持WMA和MP3)