标签:PHP
php生成随机图片文件名
通过随机数和md5算法生成一个随机图片名字,在上传图片时重命名图片名可以用到$image_name = md5(uniqid(rand())).".jpg";……
php简单的mysql查询函数
一个基本的php查询mysql数据库的函数if (!function_exists('mysql_search')) { function mysql_search($table, $columns, $query = '', $options = Array()) { if (empt……
php计算页面加载时间
简单的把开始时间放在页面头部,结束时间放在页面尾部,计算页面加载时间$start = time(); // put a long operation in heresleep(2); $diff = time() - $start; print "This page needed $diff seconds to load ……
一堆php print函数的用法汇总
此代码包含了php通过print输出不同格式的字符串的方法// report all errors:error_reporting(E_ALL); // the full path to the current fileprint __FILE__; // print the current lineprint __LINE__; /……
php中glob函数使用范例
本代码演示了php中如何通过glob查找指定的文件$dir = './'; foreach(glob($dir.'*.txt') as $file) { print $file . "\n";} /* returns: ./dummy.txt./foo.txt./i……
php在指定目录中查找指定扩展名的文件
一个php查找文件的函数,指定要查找的目录和文件扩展名function get_files_by_ext($path, $ext){ $files = array(); if (is_dir($path)){ $handle = opendir($path); while ($file = re……
php读取和写入tab分割的文件
php读取和写入tab分割的文件,包含两个独立的函数,一个读,一个写,例如cvs文件等//// save an array as tab seperated text file// function write_tabbed_file($filepath, $array, $save_keys=false){ $content = ……
php输出数组的最大值和最小值
本代码演示了php中如何通过min和max函数输出数组的最大值和最小值/* find the highest value */ print max(100, 70, 101, 50);// returns --> 101 $array = array(100, 701, 2, 4, 202);print max($array);//……
简单cURL使用范例
本代码演示了php中如何通过cURL函数抓取和下载网页function curl_download($Url){ // is cURL installed yet? if (!function_exists('curl_init')){ die('Sorry cURL is not in……
php计算整个mysql数据库的大小
php计算连接的mysql数据库的大小,用MB,KB或者GB的格式返回function CalcFullDatabaseSize($database, $db) { $tables = mysql_list_tables($database, $db); if (!$tables) { return -1; } $tab……
php Hex颜色转换成RGB颜色的方法
本范例演示了php中如何把一个HEX格式(如:#FF00FF)的颜色代码转换成RGB格式的颜色代码(如:Array(179,218,245))function Hex2RGB($color){ $color = str_replace('#', '', $color); if (strlen($c……
php根据一个给定的范围和步进生成数组
给定开始和结束值,再给定一个步进值,就可以生成一个等差数组function array_range($from, $to, $step=1){ $array = array(); for ($x=$from; $x <= $to; $x += $step){ $array[] = $x; } ……
php使用已经过去多长时间的方式显示时间
以一种可读性比较好的方式显示已经过去多长时间,比如:距离现在10秒,距离现在1天等等function time_is_older_than($t, $check_time){ $t = strtolower($t); $time_type = substr(preg_replace('/[^a-z]/', ……
php获得指定路径的最后一个参数
php获取指定路径的最后一个参数,可能是文件名或者最后一层文件夹function path_get_last_arg($path){ $path = str_replace('\\', '/', $path); $path = preg_replace('/\/+$/', &……
php随机生成易于记忆的密码
通过预定义一些单词,让php随机从这些单词中选择进行组合生成密码function random_readable_pwd($length=10){ // the wordlist from which the password gets generated // (change them as you like) $wor……
php限制文件下载速度
php限制文件下载速度,这个方案可能有一些缺陷,但可以参考一下// local file that should be send to the client$local_file = 'test-file.zip'; // filename that the user gets as default$download_fil……
php从数组随机选择元素
本代码演示了如何从php数组中随机选择一个元素显示function random_array_element(&$a){ mt_srand((double)microtime()*1000000); // get all array keys: $k = array_keys($a); // fi……
php分割合并两个字符串的函数
把两个字符串进行分割合并,例如str1=aaaa,str2=bbbb,合并后生成abababab/** * Merges two strings in a way that a pattern like ABABAB will be * the result. * * @param string $str1 String A……
php比较相似字符串
本代码演示了如何通过php的 similar_text函数比较两个字符串的相似性$word2compare = "stupid"; $words = array( 'stupid', 'stu and pid', 'hello', ……
php将带单位的文件大小字符串转换成字节数
例如:将5M或者400K这样的字符串转换成字节数字显示function StringSizeToBytes($Size){ $Unit = strtolower($Size); $Unit = preg_replace('/[^a-z]/', '', $Unit); $Value ……
一个php随机数据生产类
这是一个php专用的类,用于产生随机整数,随机字符串,随机颜色等,同时你可以对这个类进行扩展,产生自己的随机数据/// <summary> /// Helper class for generating random values /// </summary> public static class RandomHelpe……
php array_walk 调试范例
本范例演示了php中如何通过array_walk调试数组,输出一个可读性良好的格式function debug_val($val, $key='', $depth=0) { if (is_array($val)){ // call this function again with the "su……
php链接ms sql server的简单范例示范
本范例演示了php如何链接微软的sql server数据库并做简单的查询/*** Connect to database:*/ // Connect to the database (host, username, password)$con = mssql_connect('localhost','admin&……
一个把颜色变深的php函数
这个php函数可以把指定的颜色变得更深一些function ColorDarken($color, $dif=20){ $color = str_replace('#', '', $color); if (strlen($color) != 6){ return '000000……
php转换文件大小为易于阅读的格式
php把文件大小转换成易于阅读的格式,如GB,MB,KB// A much better and accurate version can be found// in Aidan's PHP Repository: // http://aidanlister.com/repos/v/function.size_readable.php ……
php字符串和16进制编码的相互转换
16进制数据和字符串相互转换的函数function String2Hex($string){ $hex=''; for ($i=0; $i < strlen($string); $i++){ $hex .= dechex(ord($string[$i])); } return $……
php遍历类包含的所有元素
获得php类包含的所有元素以key-value的形式输出class MyTestClass{ const TESTVAR1 = 1001; const TESTVAR2 = 1002; const TESTSTR1 = 'hello';} $rc = new ReflectionClass('M……
php恢复数组的key为数字序列
php把数组的key值恢复成类似于0,1,2,3,4,5…这样的数字序列function restore_array($arr){ if (!is_array($arr)){ return $arr; } $c = 0; $new = array(); while (list($key, $value) = each……
php实现每天自动变换的随机问候语
预先定义一个php数组,里面存放一些随机问候语,调用的时候指定是按照天,月还是年来自动更换问候语,如果选择月,则会每月更换一条问候语显示,不用每个月手动更换了,并且这段php代码比使用JS实现对搜索引擎友好function RandomQuoteByInterval($TimeBase, $QuotesArray){ // Make sur……
php Wordwrap用法示范
本范例演示了php中wordwrap如何按照用户指定的长度进行换行// create a long text for testing:$long_text = 'This is a long text to demonstrate the usage of the ';$long_text .= 'wordwrap f……
自定义一个简单的php hash函数
php实现的一个简单hash算法,可以用来加密,不过这个函数过于简单,不能用来解密function SimpleHash($str){ $n = 0; // The magic happens here: // I just loop trough all letters and add the // A……
php对象和数组的相互转换 Array2object and Object2array
这是2个php 匿名对象和数组相互转换的函数function array2object($array) { if (is_array($array)) { $obj = new StdClass(); foreach ($array as $key => $val){ $ob……
php定时删除文件的代码
这段代码可以在指定的时间过后删除指定的文件,用这段代码来清除缓存文件和临时文件再好不过了。// Define the folder to clean// (keep trailing slashes)$captchaFolder = 'temp/'; // Filetypes to check (you can also ……
一些php获取系统变量的方法
通过一些php内置函数获得各种系统变量的方法// get all defined variables:$v = get_defined_vars();print_r($v); // get all defined objects$v = get_object_vars();print_r($v); // classicphpinfo(……
php获取来访页面地址(referrer)
$_SERVER[‘HTTP_REFERER’]获取来访页面的地址$referer = $_SERVER['HTTP_REFERER']……
php MD5-based block cipher
Below is a MD5-based block cipher (MDC-like), which works in 128bit CFB mode.It is very useful to encrypt secret data before transfer it over the network.function md5_encrypt($p……
PHP中的 flock 文件锁使用代码
PHP中的 flock 文件锁使用代码 flock (PHP 4, PHP 5) flock — 轻便的咨询文件锁定 说明&nb……
php获取两个中间字符串
给定字符串,开始字符和结束字符获取中间的字符串function GetBetween($content,$start,$end){ $r = explode($start, $content); if (isset($r[1])){ $r = explode($end, $r[1]); return $r……
PHP获取Exif 缩略图
本代码演示了php如何获取Exif图片的缩略图// file to read$file = 'test.jpg'; $image = exif_thumbnail($file, $width, $height, $type); // width, height and type get filled with data/……
php插入数组但不影响原有顺序
php插入数组但不影响原有顺序function array_intsort($array,$num) { $array_right = $array_left = array(); $length = count($array); if ($num < $array[0]) { array_uns……
php生成25位长度的随机码
php随机字符生成代码,可以指定长度<?php # This particular code will generate a random string # that is 25 charicters long 25 comes from the number # that is in the for loop $string = &qu……