Skip to content

Instantly share code, notes, and snippets.

@xdstack
xdstack / gist:3713330
Created September 13, 2012 10:00
PHP 生成随机字符串
function genRandomString() {
// 验证码的长度
$length = 10;
// 验证码包含的字符
$characters = ’0123456789abcdefghijklmnopqrstuvwxyz’;
$string = '';
@xdstack
xdstack / gist:3713345
Created September 13, 2012 10:05
PHP 文件下载函数
// $file = "/folder/filename.ext";
function force_download($file) {
// 截取文件扩展名
$ext = explode(".", $file);
switch($ext[sizeof($ext)-1]) {
case 'jar': $mime = "application/java-archive"; break;
case 'zip': $mime = "application/zip"; break;
case 'jpeg': $mime = "image/jpeg"; break;
case 'jpg': $mime = "image/jpg"; break;
case 'jad': $mime = "text/vnd.sun.j2me.app-descriptor"; break;
@xdstack
xdstack / gist:3713539
Created September 13, 2012 10:49
php查询IP地理位置(dat数据库)
header("content-type:text/html;charset=utf-8");
//*
//文件头 [第一条索引的偏移量 (4byte)] + [最后一条索引的偏移地址 (4byte)] 8字节
//记录区 [结束ip (4byte)] + [地区1] + [地区2] 4字节+不定长
//索引区 [开始ip (4byte)] + [指向记录区的偏移地址 (3byte)] 7字节
//注意:使用之前请去网上下载纯真IP数据库,并改名为 "CoralWry.dat" 放到当前目录下即可.
//by 查询吧 www.query8.com
//*
class ipLocation {
@xdstack
xdstack / gist:3713543
Created September 13, 2012 10:49
php 去除html标签和CSS等
Function ClearHtml($content) {  
   $content = preg_replace("/<a[^>]*>/i", "", $content);  
   $content = preg_replace("/<\/a>/i", "", $content);  
   $content = preg_replace("/<div[^>]*>/i", "", $content);  
   $content = preg_replace("/<\/div>/i", "", $content);      
   $content = preg_replace("/<!--[^>]*-->/i", "", $content);//注释内容  
   $content = preg_replace("/style=.+?['|\"]/i",'',$content);//去除样式  
   $content = preg_replace("/class=.+?['|\"]/i",'',$content);//去除样式  
   $content = preg_replace("/id=.+?['|\"]/i",'',$content);//去除样式    
   $content = preg_replace("/lang=.+?['|\"]/i",'',$content);//去除样式      
@xdstack
xdstack / gist:3713586
Created September 13, 2012 11:01
PHP检验ISBN码的函数
function isbn_sum($isbn, $len)
{
/*
 * 该函数用于计算ISBN加权和
 * 参数说明:
 *   $isbn : isbn码
 *   $len  : isbn码长度
 */
    $sum = 0;
    if ($len == 10)
@xdstack
xdstack / gist:3713666
Created September 13, 2012 11:22
PHP 自动将url转为超链接
$url = "代码库 (https://gist.github.com/wdd2007)";
$url = preg_replace("#http://([A-z0-9./-]+)#", '$0', $url);
@xdstack
xdstack / gist:3713862
Created September 13, 2012 11:55
php实现汉字转拼音
$d=array(
array("a",-20319),
array("ai",-20317),
array("an",-20304),
array("ang",-20295),
array("ao",-20292),
array("ba",-20283),
array("bai",-20265),
array("ban",-20257),
array("bang",-20242),
@xdstack
xdstack / gist:3714087
Created September 13, 2012 12:44
PHP 中文编码集合类库
<?php
/**
* 中文编码集合类库
*
* 目前该类库可以实现,简体中文 <-> 繁体中文编码互换,简体中文、繁体中文 -> 拼音单向转换,
* 简体中文、繁体中文 <-> UTF8 编码转换,简体中文、繁体中文 -> Unicode单向转换
*
* @作者 Hessian(solarischan@21cn.com)
* @版本 1.5
@xdstack
xdstack / gist:3714131
Created September 13, 2012 12:57
PHP 将颜色的HEX值转换成RGB值
function hex2rgb( $colour ) {
        if ( $colour[0] == '#' ) {
                $colour = substr( $colour, 1 );
        }
        if ( strlen( $colour ) == 6 ) {
                list( $r, $g, $b ) = array( $colour[0] . $colour[1], $colour[2] . $colour[3], $colour[4] . $colour[5] );
        } elseif ( strlen( $colour ) == 3 ) {
                list( $r, $g, $b ) = array( $colour[0] . $colour[0], $colour[1] . $colour[1], $colour[2] . $colour[2] );
        } else {
                return false;
@xdstack
xdstack / gist:3714143
Created September 13, 2012 13:02
php 发送带Html格式Email
/*
EXAMPLE: htmlmail('user@domain.com', 'Look ma, HTML e-mails','You just got <a href="http://www.yougotrickrolled.com/">Rick Rolled</a>');
NOTE: $headers is optional, but can be used to set From, CC, etc.
*/
 
function htmlmail($to, $subject, $message, $headers = NULL)
{
        $mime_boundary = md5(time());
 
        $headers .= "\nMessage-ID: <" . time() . " TheSystem@{$_SERVER['SERVER_NAME']}>\n";