【ACCESS精品源码栏目提醒】:本文主要为网学会员提供“【精品】实用便利的PHP代码摘录 - 其它资料”,希望对需要【精品】实用便利的PHP代码摘录 - 其它资料网友有所帮助,学习一下!
1. PHP 可阅读随机字符串 此代码将创建一个可阅读的字符串,使其更接近词典中的单词,实用且具有密码验证功能。
/ length - length of random string must be a multiple of 2 / function readable_random_stringlength 6 consoarraybcdfghjkl mnprstvwxyz vocalarrayaeiou password srand doublemicrotime1000000 max length/2 fori1 iname //prints foo echo obj-interest1 //prints php 8. PHP 解析 XML 数据//xml stringxml_stringFoofoobar.comFoobarfoobarfoo.com//load the xml string using simplexmlxml simplexml_load_stringxml_string//loop through the each node of userforeach xml-user as user//
access attributeecho userid //subnodes are accessed by - operatorecho user-name echo user-email 9. PHP 创建日志缩略名 创建用户友好的日志缩略名。
function create_slugstringslugpreg_replace/A-Za-z0-9-/ - stringreturn slug 10. PHP 获取客户端真实 IP 地址 该函数将获取用户的真实 IP 地址,即便他使用代理服务器。
function getRealIpAddr if emptyempty_SERVERHTTP_CLIENT_IP ip_SERVERHTTP_CLIENT_IP elseif emptyempty_SERVERHTTP_X_FORWARDED_FOR //to check ip is pass from proxy ip_SERVERHTTP_X_FORWARDED_FOR else ip_SERVERREMOTE_ADDR return ip 11. PHP 强制性文件下载 为用户提供强制性的文件下载功能。
/file - path to file/function force_downloadfileif issetfilefile_existsfile headerContent-length: .filesizefileheaderContent-Type: application/octet-streamheaderContent-Disposition: attachment filename . file . readfilefile 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 - minimumCountcloudHTML cloudTags arrayspread 0 spread 1foreach data as tag count size minFontSize count - minimumCount maxFontSize - minFontSize / spreadcloudTags . htmlspecialchars stripslashes tag . return join n cloudTags . n/ Sample usage /arr ArrayActionscript 35 Adobe 22 Array 44 Background 43Blur 18 Canvas 33 Class 15 Color Palette 11 Crop 42Delimiter 13 Depth 34 Design 8 Encode 12 Encryption 30Extract 28 Filters 42echo getCloudarr 12 36 13. PHP 寻找两个字符串的相似性 PHP 提供了一个极少使用的 similar_text 函数,但此函数非常有用,用于比较两个字符串并返回相似程度的百分比。
similar_textstring1 string2 percent //percent will have the percentage of similarity 14. PHP 在应用程序中使用 Gravatar 通用头像 随着 WordPress 越来越普及,Gravatar 也随之流行。
由于 Gravatar 提供了易于使用的 API,将其纳入应用程序也变得十分方便。
/ email - Email address to show gravatar for size - size of gravatar default - URL of default gravatar to use rating - rating of GravatarG PG R X / function show_gravataremail size default rating echo 15. PHP 在字符断点处截断文字 所谓断字 word break,即一个单词可在转行时断开的地方。
这一函数将在断字处截断字符串。
// Original PHP code by Chirp Internet: www.chirp.com.au// Please acknowledge use of this code by including this header.function myTruncatestring limit break. pad... // return with no change if string is shorter than limitifstrlenstring opendestinationoverwrite ZIPARCHIVE::OVERWRITE : ZIPARCHIVE::CREATE true return false//add the filesforeachvalid_files as file zip-addFilefilefile//debug//echo The zip archive contains zip-numFiles files with a status of zip-status//close the zip -- donezip-close//check to make sure the file existsreturn file_existsdestinationelsereturn false/ Example Usage /filesarrayfile1.jpg file2.jpg file3.gifcreate_zipfiles myzipfile.zip true 17. PHP 解压缩 Zip 文件/file - path to zip filedestination - destination directory for unzipped files/function unzip_filefile destination// create objectzip new ZipArchive // open archive if zip-openfile TRUE die ’Could not open archive’ // extract contents to destination directory zip-extractTodestination // close archive zip-close echo Archive extracted to directory 18. PHP 为 URL 地址预设 http 字符串 有时需要接受一些表单中的网址输入,但用户很少添加 http:// 字段,此代码将为网址添加该字段。
if preg_match/httpftp:/ _POSTurl _POSTurl http://._POSTurl 19. PHP 将网址字符串转换成超级链接 该函数将 URL 和 E-mail 地址字符串转换为可点击的超级链接。
function makeClickableLinkstext text eregi_replacefht1tp://-a-zA-Z0-9:_.// 1 text text eregi_replace:space:www.-a-zA-Z0-9:_.// 12 text text eregi_replace_.0-9a-z-0-9a-z0-9a-z-.a-z23 1 textreturn text 20. PHP 调整图像尺寸 创建图像缩略图需要许多时间,此代码将有助于了解缩略图的逻辑。
/filename - path to the imagetmpname - temporary path to thumbnailxmax - max widthymax - max height/function resize_imagefilename tmpname xmax ymax ext explode. filename ext extcountext-1 ifext jpg ext jpeg im imagecreatefromjpegtmpname elseifext png im imagecreatefrompngtmpname elseifext gif im imagecreatefromgiftmpname x imagesxim y imagesyim ifx