Linux安全网 - Linux操作系统_Linux 命令_Linux教程_Linux黑客

会员投稿 投稿指南 本期推荐:
搜索:
您的位置: Linux安全网 > Linux编程 > » 正文

21个实用便利的PHP代码(2)

来源: 未知 分享至:

$json_string=\'{\"id\":1,\"name\":\"foo\",\"email\":\"foo@foobar.com\",\"interest\":[\"wordpress\",\"php\"]} \'; $obj=json_decode($json_string); echo $obj->name; //prints foo echo $obj->interest[1]; //prints php

8. PHP解析 XML 数据

//xml string $xml_string=\"<?xml version=\'1.0\'?> <users> <user id=\'398\'> <name>Foo</name> <email>foo@bar.com</name> </user> <user id=\'867\'> <name>Foobar</name> <email>foobar@foo.com</name> </user> </users>\"; //load the xml string using simplexml $xml = simplexml_load_string($xml_string); //loop through the each node of user foreach ($xml->user as $user) { //access attribute echo $user[\'id\'], \' \'; //subnodes are accessed by -> operator echo $user->name, \' \'; echo $user->email, \'<br />\'; }

9. PHP创建日志缩略名

创建用户友好的日志缩略名。

function create_slug($string){ $slug=preg_replace(\'/[^A-Za-z0-9-]+/\', \'-\', $string); return $slug; }

10. PHP获取客户端真实 IP 地址

该函数将获取用户的真实 IP 地址,即便他使用代理服务器。

function getRealIpAddr() { if (!emptyempty($_SERVER[\'HTTP_CLIENT_IP\'])) { $ip=$_SERVER[\'HTTP_CLIENT_IP\']; } elseif (!emptyempty($_SERVER[\'HTTP_X_FORWARDED_FOR\'])) //to check ip is pass from proxy { $ip=$_SERVER[\'HTTP_X_FORWARDED_FOR\']; } else { $ip=$_SERVER[\'REMOTE_ADDR\']; } return $ip; }

11. PHP强制性文件下载

为用户提供强制性的文件下载功能。

/******************** *@file - path to file */ function force_download($file) { if ((isset($file))&&(file_exists($file))) { header(\"Content-length: \".filesize($file)); header(\'Content-Type: application/octet-stream\'); header(\'Content-Disposition: attachment; filename=\"\' . $file . \'\"\'); readfile(\"$file\"); } else { echo \"No file selected\"; } }

12. PHP创建标签云

function getCloud( $data = array(), $minFontSize = 12, $maxFontSize = 30 ) { $minimumCount = min( array_values( $data ) ); $maximumCount = max( array_values( $data ) ); $spread = $maximumCount - $minimumCount; $cloudHTML = \'\'; $cloudTags = array(); $spread == 0 && $spread = 1; foreach( $data as $tag => $count ) { $size = $minFontSize + ( $count - $minimumCount ) * ( $maxFontSize - $minFontSize ) / $spread; $cloudTags[] = \'<a style=\"font-size: \' . floor( $size ) . \'px\' . \'\" href=\"#\" title=\"\'\' . $tag . \'\' returned a count of \' . $count . \'\">\' . htmlspecialchars( stripslashes( $tag ) ) . \'</a>\'; } return join( \"n\", $cloudTags ) . \"n\"; } /************************** **** Sample usage ***/ $arr = Array(\'Actionscript\' => 35, \'Adobe\' => 22, \'Array\' => 44, \'Background\' => 43, \'Blur\' => 18, \'Canvas\' => 33, \'Class\' => 15, \'Color Palette\' => 11, \'Crop\' => 42, \'Delimiter\' => 13, \'Depth\' => 34, \'Design\' => 8, \'Encode\' => 12, \'Encryption\' => 30, \'Extract\' => 28, \'Filters\' => 42); echo getCloud($arr, 12, 36);

13. PHP寻找两个字符串的相似性

PHP 提供了一个极少使用的 similar_text 函数,但此函数非常有用,用于比较两个字符串并返回相似程度的百分比。

similar_text($string1, $string2, $percent); //$percent will have the percentage of similarity

14. PHP在应用程序中使用 Gravatar 通用头像

随着 WordPress 越来越普及,Gravatar 也随之流行。由于 Gravatar 提供了易于使用的 API,将其纳入应用程序也变得十分方便。


Tags:
分享至:
最新图文资讯
1 2 3 4 5 6
验证码:点击我更换图片 理智评论文明上网,拒绝恶意谩骂 用户名:
关于我们 - 联系我们 - 广告服务 - 友情链接 - 网站地图 - 版权声明 - 发展历史