此代码会从指定的服务器文件夹随机选择一个图片进行显示,非常有用,图片格式为.gif,.jpg,.png<?php //This will get an array of all the gif, jpg and png images in a folder $img_array = glob("/path/to/images/*.{……继续阅读 » 水墨上仙 4年前 (2021-03-10) 1493浏览 306个赞
本代码可以根据用户输入的英文姓名,分析出姓名的首字母输出,比如”Billy Bob” to “B.B.”<?php function initials($name){ $nword = explode(" ",$name); foreach($nword a……继续阅读 » 水墨上仙 4年前 (2021-03-10) 1266浏览 1648个赞
这是一个经常用到的功能,如果字符串太长,需要按照指定的长度进行截断,然后在后面加上三个点的省略号<?php //if a string is longer than the defined length, //it will add 3 periods to the end of the string. //you can change ……继续阅读 » 水墨上仙 4年前 (2021-03-10) 2459浏览 2120个赞
一个简单的php生成的图片验证码,带表单,验证码生成代码,以及验证程序image.php<?php header("Content-type: image/png"); $string = "abcdefghijklmnopqrstuvwxyz0123456789"; for($i=0;$i<……继续阅读 » 水墨上仙 4年前 (2021-03-10) 1807浏览 680个赞
php汉字转换成拼音的代码类<?phpfunction Pinyin($_String, $_Code='UTF8'){ //GBK页面可改为gb2312,其他随意填写为UTF8 $_DataKey = "a|ai|an|ang|ao|ba|bai|ban|bang|bao|bei|ben|beng……继续阅读 » 水墨上仙 4年前 (2021-03-10) 2105浏览 2197个赞
php计算一个文件的大小<?php function dirSize($directoty){ $dir_size=0; if($dir_handle=@opendir($directoty)) { while($filename=readdir($dir_handle)){ ……继续阅读 » 水墨上仙 4年前 (2021-03-10) 1997浏览 457个赞
php上传图片的同时生成缩略图<?php function createThumbnail($imageDirectory, $imageName, $thumbDirectory, $thumbWidth, $quality){ $details = getimagesize("$imageDirectory/$image……继续阅读 » 水墨上仙 4年前 (2021-03-10) 2737浏览 1307个赞
列出目录的全部子目录,存储在数组里<?php function listdir($dir){ if ($handle = opendir($dir)){ $output = array(); while (false !== ($item = readdir($handle))){ ……继续阅读 » 水墨上仙 4年前 (2021-03-10) 1363浏览 478个赞
一个简单的php获得文件扩展名的方法,如:.php<?php $file = '/path/to/file.php'; $info = pathinfo($file); echo $info['extension']; ?>……继续阅读 » 水墨上仙 4年前 (2021-03-10) 1806浏览 2970个赞
PHP面向对象实现数据库登陆的类代码 db_class.php类的实现代码:<?php class dbclass { public $connection ; public $result ; public $fetch_num ;……继续阅读 » 水墨上仙 4年前 (2021-03-10) 1749浏览 2908个赞
php获取post过来的xml数据并解析 php 如何获取请求的xml数据,对方通过http协议post提交过来xml数据,php如何获取到这些数据呢?<?php $xml_data ='<AATAvailReq1>'. ……继续阅读 » 水墨上仙 4年前 (2021-03-10) 1596浏览 1644个赞
大部分计算机软件支持的是RGB格式的颜色,但是HSL会更加人性化,下面的代码实现了rgb和hsl的相互转换def HSL_to_RGB(h,s,l): ''' Converts HSL colorspace (Hue/Saturation/Value) to RGB colorspace. Form……继续阅读 » 水墨上仙 4年前 (2021-03-10) 1740浏览 702个赞
Linux系统下php获得系统分区信息的代码$pars = array_filter(explode("\n",`df -h`)); foreach ($pars as $par) { if ($par{0} == '/') { $_tmp = array_values(ar……继续阅读 » 水墨上仙 4年前 (2021-03-10) 2780浏览 2848个赞
本代码演示了php中如何使用转义字符<?php print("<font color=\"red\">Red text</font>"); ?> ……继续阅读 » 水墨上仙 4年前 (2021-03-10) 1870浏览 2051个赞
当我们在接受未知客户端提交的数据,由于各客户端的编码不统一,但在我们的服务器端最终只能以一种编码方式来处理,这种情况下就会涉及到编码转换问题// 自动转换字符集 支持数组转换 function auto_charset($fContents, $from='gbk', $to='utf-8') { $from……继续阅读 » 水墨上仙 4年前 (2021-03-10) 2865浏览 1944个赞
php调用万网接口实现域名查询 今天给别人做网站时有个需求是要有域名查询功能,查了点资料写了个简单的查询功能 前台页面用的是checkbox,代码如下:<form name="form1" method="post" ……继续阅读 » 水墨上仙 4年前 (2021-03-10) 3100浏览 1214个赞
使用python打包你的整个Gmail邮箱,需要Gmail打开IMAP服务#!/usr/bin/python# -*- coding: iso-8859-1 -*-""" GMail archiver 1.1This program will download and archive all you emails f……继续阅读 » 水墨上仙 4年前 (2021-03-10) 1559浏览 626个赞
在PHP编程中,递归调用常常与静态变量使用。静态变量的含义可以参考PHP手册.希望下面的代码,会更有利于对递归以及静态变量的理解<?php //下面代码会画出一个很漂亮的叶子 // 定义 PI 一分的角度的值 define("PII", M_PI/180); // 新建图像资源,并定义其背景为 白色,前景色为 黑色 ……继续阅读 » 水墨上仙 4年前 (2021-03-10) 2484浏览 1524个赞
python通过urllib2发送post请求并抓取返回内容#!/usr/bin/pythonimport urllib,urllib2url = 'http://www.commentcamarche.net/search/search.php3'parameters = {'Mot' : 'G……继续阅读 » 水墨上仙 4年前 (2021-03-10) 1958浏览 1288个赞
本代码通过查询那些提供ip地址查看的网站获取你自己的真实ip地址,如果你当前离线,则返回None范例:>>> print public_ip()85.212.182.25import urllib,random,reip_regex = re.compile("(([0-9]{1,3}\.){3}[0-9]{1,3})")def……继续阅读 » 水墨上仙 4年前 (2021-03-10) 1838浏览 2700个赞
php计算指定目录(包含子目录)的完整大小/** * Calculate the full size of a directory * * @author Jonas John * @version 0.2 * @link http://www.jonasjohn.de/snippets/php/dir-siz……继续阅读 » 水墨上仙 4年前 (2021-03-10) 1520浏览 1144个赞
一个通过break语句终止php循环的例子$max_loop=5; //This is the desired value of Looping$count = 0; //First we set the count to be zeoecho "<h2> Here goes the values</h2>&quo……继续阅读 » 水墨上仙 4年前 (2021-03-10) 1748浏览 1908个赞
php对字符串在指定的长度范围内进行随机分割,把分割后的结果存在数组里面function RandomSplit($min, $max, $str){ $a = array(); while ($str != ''){ $p = rand($min, $max); $p = ($p……继续阅读 » 水墨上仙 4年前 (2021-03-10) 2643浏览 2880个赞
对文件夹和文件递归之行chmod命令来改变执行权限<? function recursiveChmod($path, $filePerm=0644, $dirPerm=0755) { // Check if the path exists if(!file_exists($path)) { ……继续阅读 » 水墨上仙 4年前 (2021-03-10) 1795浏览 2845个赞
自动匹配页面里的网址,包含http,ftp等,自动给网址加上链接function text2links($str='') { if($str=='' or !preg_match('/(http|www\.|@)/i', $str)) { return $str; } ……继续阅读 » 水墨上仙 4年前 (2021-03-10) 2743浏览 1099个赞
模拟post数据到其它web serverfunction post_request($url, $data, $referer='') { // Convert the data array into URL Parameters like a=b&foo=bar etc. $data = http_b……继续阅读 » 水墨上仙 4年前 (2021-03-10) 2823浏览 1804个赞
我们一般会在页面下方输出版权信息,包含年份信息,每年都要修改,这段简单的代码帮你解决这个问题,自动更新年份function autoUpdatingCopyright($startYear){ // given start year (e.g. 2004) $startYear = intval($startYear); ……继续阅读 » 水墨上仙 4年前 (2021-03-10) 1325浏览 1392个赞
一段自定义的php生成uuid的代码<?php$u=uuid(); // 0001-7f000001-478c8000-4801-47242987echo $u;echo "<br>";print_r(uuidDecode($u)); // Array ( [serverID] => 0001 [……继续阅读 » 水墨上仙 4年前 (2021-03-10) 2707浏览 689个赞
php通过joomla jmail 类使用 gmail smtp 账号发送邮件<?phpjimport('joomla.mail.mail');$mail = new JMail();$mail->setsender("useremail");$mail->addRecipient(&q……继续阅读 » 水墨上仙 4年前 (2021-03-10) 3004浏览 597个赞
php上传单个文件到ftp服务器的演示范例// FTP access parameters$host = 'ftp.example.org';$usr = 'example_user';$pwd = 'example_password'; // file to move:$loca……继续阅读 » 水墨上仙 4年前 (2021-03-10) 2195浏览 2609个赞
有时候网站的www域名和非www域名都能访问网站,但是这样不利于搜索引擎的收录,会分散网页的权重,所以希望用户访问非www的域名时通过301永久重定向到www域名,例如用户访问75271.com会直接转向www.75271.com,本php代码考虑了无法通过head重定向的情况,会在页面上输出链接,让用户点击。// Install info.:// ……继续阅读 » 水墨上仙 4年前 (2021-03-10) 1303浏览 1042个赞
php正则表达式功能强大,本范例演示了preg_replace_callback函数的用法// Define a dummy text, for testing...$Text = "Title: Hello world!\n";$Text .= "Author: Jonas\n";$Text .= &q……继续阅读 » 水墨上仙 4年前 (2021-03-10) 1666浏览 998个赞
本代码演示了php中header函数的详细用法// See related links for more status codes // Use this header instruction to fix 404 headers// produced by url rewriting...header('HTTP/1.1 200 O……继续阅读 » 水墨上仙 4年前 (2021-03-10) 1167浏览 2930个赞
php通过查询whois信息的网站列表进行查询function whois_query($domain) { // fix the domain name: $domain = strtolower(trim($domain)); $domain = preg_replace('/^http:\/\//i'……继续阅读 » 水墨上仙 4年前 (2021-03-10) 1632浏览 1012个赞
php获得指定范围内的最接近的数// Returns the next higher or lower numberfunction NextRelatedNumber($number, $range){ $r = $number % $range; $f = $number - $r; $b = round($r /……继续阅读 » 水墨上仙 4年前 (2021-03-10) 2817浏览 1060个赞
php链接mysql数据库,查询范例/*** Connect to database:*/ // connect to the database$con = mysql_connect('localhost','testuser','testpassword') or di……继续阅读 » 水墨上仙 4年前 (2021-03-10) 1723浏览 685个赞
php使用GD创建保持宽高比的缩略图/** * Create a thumbnail image from $inputFileName no taller or wider than * $maxSize. Returns the new image resource or false on error. * Auth……继续阅读 » 水墨上仙 4年前 (2021-03-10) 1462浏览 2335个赞
本代码传入两个数组A和B,返回A-B的结果,即挑选出存在于A,但不存在于B的元素<?php function RestaDeArrays($vectorA,$vectorB) { $cantA=count($vectorA); $cantB=count($vectorB); ……继续阅读 » 水墨上仙 4年前 (2021-03-10) 2248浏览 450个赞
传入用户提交的参数时使用这段代码提供的函数先对参数进行处理,然后传入sql语句,用:mysqlquery(“INSERT INTO table VALUES(‘” . sqlsanitize($_POST[“variable”) . “‘)”);替代:mysqlq……继续阅读 » 水墨上仙 4年前 (2021-03-10) 1629浏览 1641个赞
php递归遍历删除文件,这个函数稍加修改就可以变成一个递归文件拷贝函数<? function mover($src,$dst) { $handle=opendir($src); // Opens source dir. if (!is_dir($dst)) mkdir($dst,0755); ……继续阅读 » 水墨上仙 4年前 (2021-03-10) 1355浏览 2725个赞
php阿拉伯数字和罗马数字相互转换<?php // Function that calculates the roman string to the given number: function dec2roman($f) { // Return false if eithe……继续阅读 » 水墨上仙 4年前 (2021-03-10) 2202浏览 910个赞
php将任意进制的数转换成10进制,例如8进制转换成10进制,16进制转换成10进制<?php # Show the steps involved in converting a number # from any base (like octal or hex) to base 10 # See below for examples,……继续阅读 » 水墨上仙 4年前 (2021-03-10) 2275浏览 1995个赞
给定一个字符串和排列组合长度生成所有可能的排列组合<? function permutations($letters,$num){ $last = str_repeat($letters{0},$num); $result = array(); while($last != str_repeat(lastchar(……继续阅读 » 水墨上仙 4年前 (2021-03-10) 1600浏览 1466个赞
php关联数组快速排序<? function qsort($a,$f) { qsort_do(&$a,0,Count($a)-1,$f); } function qsort_do($a,$l,$r,$f) { if ($l < $r) { qsort_partit……继续阅读 » 水墨上仙 4年前 (2021-03-10) 3141浏览 2107个赞
一个用来防止sql注入的参数检查函数function cleanuserinput($dirty){ if (get_magic_quotes_gpc()) { $clean = mysql_real_escape_string(stripslashes($dirty)); }else{ $clean = mysql_real_esca……继续阅读 » 水墨上仙 4年前 (2021-03-10) 2081浏览 2608个赞