最精简高效的PHP数组转HTML代码的方法array2html
2014-02-14 13:17
441 查看
目前网络上常规的 PHP 数组转 HTML 代码的方法都是通过循环 for 或者 foreach 来遍历数组,然后通过字符替换、正则表达式替换或者字符拼接等方法来生成 HTML 代码,为了避免使用循环而发明此方法,搜了一下目前网络上没有人尝试过这种方法,属于一次新的尝试,我把它命名为array2html。
Methods at present on the network PHP array of conventional HTML code is through for or foreach loop to iterate through the array, and then through the character replacement, regular expression replacement or character stitching method to generate HTML code,
in order to avoid the use of the array loop and invented this method, search the Internet and no one tried this approach, is a new attempt, I named it array2html.
PHP 函数 array2html 的代码:
The source code of function 'array2html' is:
输出结果:
The result of output like:
此文属个人原创,转载请注明来源!
Methods at present on the network PHP array of conventional HTML code is through for or foreach loop to iterate through the array, and then through the character replacement, regular expression replacement or character stitching method to generate HTML code,
in order to avoid the use of the array loop and invented this method, search the Internet and no one tried this approach, is a new attempt, I named it array2html.
PHP 函数 array2html 的代码:
The source code of function 'array2html' is:
<?php /** * Replace PHP Array to HTML Code directly * @param array $arr ref of input array data * @param string $mainTag top html tag name of output html code * @param string $listTag html tag name of each element * @author csdn_do_net_id:hpchn * @link http://blog.csdn.net/hpchn */ function array2html(array &$arr, $mainTag='ul', $listTag='li') { return strtr(var_export($arr,TRUE) ,array( "'," => "</$listTag>", " => '" => '>', ' ' => "<$listTag id=", 'array (' => "<$mainTag>", "\n)" => "\n</$mainTag>") ); } echo array2html($_SERVER);
输出结果:
The result of output like:
<ul> <li id='SERVER_PROTOCOL'>HTTP/1.1</li> <li id='GATEWAY_INTERFACE'>CGI/1.1</li> <li id='SERVER_SOFTWARE'>nginx/1.5.9</li> <li id='REMOTE_ADDR'>127.0.0.1</li> <li id='REMOTE_PORT'>53937</li> <li id='SERVER_ADDR'>127.0.0.1</li> <li id='SERVER_PORT'>80</li> <li id='SERVER_NAME'>localhost</li> <li id='REDIRECT_STATUS'>200</li> <li id='HTTP_ACCEPT'>text/html, application/xhtml+xml, */*</li> <li id='HTTP_ACCEPT_LANGUAGE'>zh-CN</li> <li id='HTTP_USER_AGENT'>Mozilla/5.0 (compatible; MSIE 9.0; Windows NT 6.1; WOW64; Trident/5.0)</li> <li id='HTTP_ACCEPT_ENCODING'>gzip, deflate</li> <li id='HTTP_HOST'>localhost</li> <li id='HTTP_CONNECTION'>Keep-Alive</li> </ul>
此文属个人原创,转载请注明来源!
相关文章推荐
- PHP正则删除HTML代码中宽高样式的方法
- php页面嵌套html代码,,html中js调用php中的数组
- php下将多个数组合并成一个数组的方法与实例代码
- PHP HTML JavaScript MySQL代码如何互相传值的方法分享
- php代码从html代码中提取出所有的图片返回数组
- php 将文本区域空格、换行转换成HTML代码后插入数据库的方法
- 将php数组输出html表格的方法
- php将HTML表格每行每列转为数组实现采集表格数据的方法
- 让PHP代码更危险----使用别的系统命令--如sql语句--exec(),system()方法甚至html的js语句
- PHP遍历数组的代码,三种方法
- php数组生成html下拉列表的方法
- 将php数组输出html表格的方法
- 递归遍历多维数组(树数据结构)的超级简单方式,并且可以递归超过200层,摘自<<PHP精粹:编写高效PHP代码>>
- php获取从html表单传递数组的方法
- php 批量生成html,txt文件的方法(实例代码)
- php数组生成html下拉列表的方法
- PHP正则删除html代码中a标签并保留标签内容的方法[原创]_php技巧_脚本之家
- php下将多个数组合并成一个数组的方法与实例代码
- PHP HTML JavaScript MySQL代码如何互相传值的方法分享
- php实现有序数组打印或排序的方法【附Python、C及Go语言实现代码】