function.inc.php超越php


Posted in PHP onDecember 09, 2006

<?php
/**
 * Global Function
 *
 * @author   Avenger <avenger@php.net>
 * @version 1.14 $Id 2003-05-30 10:10:08 $
 */
/**
 * 弹出提示框
 *
 * @access public
 * @param string $txt 弹出一个提示框,$txt为要弹出的内容
 * @return void
 */
function popbox($txt) {
    echo "<script language='JavaScript'>alert('".$txt."')</script>";
}
/**
 * 非法操作警告
 *
 * @access public
 * @param string $C_alert   提示的错误信息
 * @param string $I_goback  返回后返回到哪一页,不指定则不返回
 * @return void
 */
function alert($C_alert,$I_goback='main.php') {
    if(!empty($I_goback)) {
        echo "<script>alert('$C_alert');window.location.href='$I_goback';</script>";
    } else {
        echo "<script>alert('$C_alert');</script>";
    }
}
/**
 * 产生随机字符串
 *
 * 产生一个指定长度的随机字符串,并返回给用户
 *
 * @access public
 * @param int $len  产生字符串的位数
 * @return string 
 */
function randstr($len=6) {
    $chars='ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789-@#~'; // characters to build the password from
    mt_srand((double)microtime()*1000000*getmypid()); // seed the random number generater (must be done)
    $password='';
    while(strlen($password)<$len)
        $password.=substr($chars,(mt_rand()%strlen($chars)),1);
    return $password;
}
/**
 * 判断下拉菜音的选取项
 *
 * 可以判断字符串一和字符串二是否相等.从而使相等的项目在下拉菜单中被选择
 *
 * @access public
 * @param string $str1  要比较的字符串一
 * @param string $str2  要比较的字符串二
 * @return string       相等返回字符串"selected",否则返回空字符串
 */
function ckselect($str1,$str2) {
    if($str1==$str2) {
        return ' selected';
    }
    return '';
}
/**
 * 一个自定义的Ftp函数
 *
 * @access private
 * @return void
 */
function myftp($ftp_server,$ftp_port,$ftp_username,$ftp_password,$ftp_path='/') {
    $ftpid=@ftp_connect($ftp_server,$ftp_port) or die('Connect To Ftp Server Error!');
    @ftp_login($ftpid,$ftp_username,$ftp_password) or die('Login Ftp Error!');
    @ftp_chdir($ftpid,'/'.$ftp_path) or die('Chdir Error!');
    return $ftpid;
}
/**
 * 截取中文部分字符串
 *
 * 截取指定字符串指定长度的函数,该函数可自动判定中英文,不会出现乱码
 *
 * @access public
 * @param string    $str    要处理的字符串
 * @param int       $strlen 要截取的长度默认为10
 * @param string    $other  是否要加上省略号,默认会加上
 * @return string
 */
function showtitle($str,$strlen=10,$other=true) {
    $j = 0;
    for($i=0;$i<$strlen;$i++)
      if(ord(substr($str,$i,1))>0xa0) $j++;
    if($j%2!=0) $strlen++;
    $rstr=substr($str,0,$strlen);
    if (strlen($str)>$strlen && $other) {$rstr.='...';}
    return $rstr;
}
/**
 * 制作链接
 *
 * @access public
 * @param string    url         要链接到的网址
 * @param string    linktext    显示的链接文字
 * @param string    target      目标框架
 * @param string    extras      扩展参数
 * @return string
 */
function make_link ($url, $linktext=false, $target=false, $extras=false) {
    return sprintf("<a href=\"%s\"%s%s>%s</a>",
        $url,
        ($target ? ' target="'.$target.'"' : ''),
        ($extras ? ' '.$extras : ''),
        ($linktext ? $linktext : $url)
    );
}
/**
 * 格式化用户评论
 *
 * @access public
 * @param string
 * @return void
 */
function clean_note($text) {
    $text = htmlspecialchars(trim($text));
    /* turn urls into links */
    $text = preg_replace("/((mailto|http|ftp|nntp|news):.+?)(>|\s|\)|\"|\.\s|$)/","<a href=\"\1\">\1</a>\3",$text);
    /* this 'fixing' code will go away eventually. */
    $fixes = array('<br>', '<p>', '</p>');
    reset($fixes);
    while (list(,$f) = each($fixes)) {
        $text = str_replace(htmlspecialchars($f), $f, $text);
        $text = str_replace(htmlspecialchars(strtoupper($f)), $f, $text);
    }
    /* <p> tags make things look awfully weird (breaks things out of the <code>
       tag). Just convert them to <br>'s
    */
    $text = str_replace (array ('<P>', '<p>'), '<br>', $text);
    /* Remove </p> tags to prevent it from showing up in the note */
    $text = str_replace (array ('</P>', '</p>'), '', $text);
    /* preserve linebreaks */
    $text = str_replace("\n", "<br>", $text);
    /* this will only break long lines */
    if (function_exists("wordwrap")) {
        $text = wordwrap($text);
    }
    // Preserve spacing of user notes
    $text = str_replace("  ", "  ", $text);
    return $text;
}
/**
 * 获取图象信息的函数
 *
 * 一个全面获取图象信息的函数
 *
 * @access public
 * @param string $img 图片路径
 * @return array
 */
function getimageinfo($img) {
    $img_info = getimagesize($img);
    switch ($img_info[2]) {
    case 1:
    $imgtype = "GIF";
    break;
    case 2:
    $imgtype = "JPG";
    break;
    case 3:
    $imgtype = "PNG";
    break;
    }
    $img_size = ceil(filesize($img)/1000)."k";
    $new_img_info = array (
        "width"=>$img_info[0],
        "height"=>$img_info[1],
        "type"=>$imgtype,
        "size"=>$img_size
    );
    return $new_img_info;
}
/**
 * 计算当前时间
 *
 * 以微秒为单位返回当前系统的时间
 *
 * @access public
 * @return real
 */
function getmicrotime() {
    $tmp = explode(' ', microtime());
    return (real)$tmp[1]. substr($tmp[0], 1);
}
/**
 * 写文件操作
 *
 * @access public
 * @param bool
 * @return void
 */
function wfile($file,$content,$mode='w') {
    $oldmask = umask(0);
    $fp = fopen($file, $mode);
    if (!$fp) return false;
    fwrite($fp,$content);
    fclose($fp);
    umask($oldmask);
    return true;
}
/**
 * 加载模板文件
 *
 * @access public
 * @return void
 */
function tpl_load($tplfile,$path='./templates/',$empty='remove') {
    global $tpl;
    $path ? '' : $path='./templates/'; 
    require_once 'HTML/Template/PHPLIB.php';
    $tpl = new Template_PHPLIB($path,$empty);
    $tpl->setFile('main',$tplfile);
}
/**
 * 模板解析输出
 *
 * @access public
 * @return void
 */
function tpl_output() {
    global $tpl;
    $tpl->parse('output','main');
    $tpl->p('output');
}
/**
 * 邮件发送函数
 *
 * @access public private
 * @param bool
 * @return void
 */
function mailSender($from, $to, $title, $content) {
    $from ? $from = 'sender@phpe.net' : '';
    $title ? $title = 'From Exceed PHP...' : '';
    $sig = "
      感谢您使用我们的服务.\n\n
                                                Exceed PHP(超越PHP)\n
                                                $maildate\n\n
---------------------------------------------------------------------------------------
\n\n
去发现极限方法的唯一办法就是去超越它\n
超越PHP欢迎您(http://www.phpe.net)\n
";
    $content .= $sig;
    if (@mail($to, $title, $content, "From:$from\nReply-To:$from")) {
        return true;
    } else {
        return false;
    }
}
function br2none($str) {
    return str_replace(array('<br>', '<br />'), "", $str);
}
/**
 * UBB解析
 *
 * @param      none
 * @access     public
 * @return     void
*/
function ubbParse($txt, $coverhtml=0) {
    if ($coverhtml == 0) $txt = nl2br(new_htmlspecialchars($txt));  //BR和HTML转换
    //只转换BR,不转换HTML
    if ($coverhtml == 1) {
        if (!preg_match('/<\s*(p|br)\s*>/is', $txt) && !preg_match('/<table.+<\/table>/is', $txt)) {
            $txt = strip_tags($txt);
            $txt = nl2br($txt);
        } else {
            $txt = str_replace('<?', '<?', $txt);
        }
    }
    // pre and quote
    //error_reporting(E_ALL);
    $txt = preg_replace( "#\[quote\](.+?)\[/quote\]#is", "<blockquote>\1</blockquote>", $txt );
    $txt = preg_replace( "#\[code\](.+?)\[/code\]#ise", "'<pre class=php>'.br2none('').'</pre>'", $txt );
    // Colors 支持?套
    while( preg_match( "#\[color=([^\]]+)\](.+?)\[/color\]#is", $txt ) ) {
        $txt = preg_replace( "#\[color=([^\]]+)\](.+?)\[/color\]#is", "<span style='color:\1'>\2</span>", $txt );
    }
    // Align
    $txt = preg_replace( "#\[center\](.+?)\[/center\]#is", "<center>\1</center>", $txt );
    $txt = preg_replace( "#\[left\](.+?)\[/left\]#is", "<div align=left>\1</div>", $txt );
    $txt = preg_replace( "#\[right\](.+?)\[/right\]#is", "<div align=right>\1</div>", $txt );
    // Sub & sup
    $txt = preg_replace( "#\[sup\](.+?)\[/sup\]#is", "<sup>\1</sup>", $txt );
    $txt = preg_replace( "#\[sub\](.+?)\[/sub\]#is", "<sub>\1</sub>", $txt );
    // email tags
    // [email]avenger@php.net[/email]   [email=avenger@php.net]Email me[/email]
    $txt = preg_replace( "#\[email\](\S+?)\[/email\]#i"                                                                , "<a href='mailto:\1'>\1</a>", $txt );
    $txt = preg_replace( "#\[email\s*=\s*\"\;([\.\w\-]+\@[\.\w\-]+\.[\.\w\-]+)\s*\"\;\s*\](.*?)\[\/email\]#i"  , "<a href='mailto:\1'>\2</a>", $txt );
    $txt = preg_replace( "#\[email\s*=\s*([\.\w\-]+\@[\.\w\-]+\.[\w\-]+)\s*\](.*?)\[\/email\]#i"                       , "<a href='mailto:\1'>\2</a>", $txt );
    // url tags
    // [url]http://www.phpe.net[/url]   [url=http://www.phpe.net]Exceed PHP![/url]
    $txt = preg_replace( "#\[url\](\S+?)\[/url\]#i"                                       , "<a href='\1' target='_blank'>\1</a>", $txt );
    $txt = preg_replace( "#\[url\s*=\s*\"\;\s*(\S+?)\s*\"\;\s*\](.*?)\[\/url\]#i" , "<a href='\1' target='_blank'>\2</a>", $txt );
    $txt = preg_replace( "#\[url\s*=\s*(\S+?)\s*\](.*?)\[\/url\]#i"                       , "<a href='\1' target='_blank'>\2</a>", $txt );
    // Start off with the easy stuff
    $txt = preg_replace( "#\[b\](.+?)\[/b\]#is", "<b>\1</b>", $txt );
    $txt = preg_replace( "#\[i\](.+?)\[/i\]#is", "<i>\1</i>", $txt );
    $txt = preg_replace( "#\[u\](.+?)\[/u\]#is", "<u>\1</u>", $txt );
    $txt = preg_replace( "#\[s\](.+?)\[/s\]#is", "<s>\1</s>", $txt );
    // Header text
    $txt = preg_replace( "#\[h([1-6])\](.+?)\[/h[1-6]\]#is", "<h\1>\2</h\1>", $txt );
    // Images
    $txt = preg_replace( "#\[img\](.+?)\[/img\]#i", "<a href='\1' target='_blank'><img alt='Click to fullsize' src='\1' border='0' onload='javascript:if(this.width>500) this.width=500' align='center' hspace='10' vspace='10'></a><br />", $txt );
    // Attach
    $txt = preg_replace( "#\[attach\s*=\s*\"\;\s*(\S+?)\s*\"\;\s*\](.*?)\[\/attach\]#i" , "<a href='\2' target='_blank'><b>相关附件:</b>\1</a>", $txt );
    $txt = preg_replace( "#\[attach\s*=\s*(\S+?)\s*\](.*?)\[\/attach\]#i"                       , "<a href='\2' target='_blank'><b>相关附件:</b>\1</a>", $txt );
    // Iframe
    $txt = preg_replace( "#\[iframe\](.+?)\[/iframe\]#i", "<div align='center'><iframe src='\1' style='width:96%;height:400px'></iframe><br clear='all'><a href='\1' target='_blank'>在新窗口打开链接</a></div>", $txt );
    // (c) (r) and (tm)
    $txt = preg_replace( "#\(c\)#i"     , "©" , $txt );
    $txt = preg_replace( "#\(tm\)#i"    , "™" , $txt );
    $txt = preg_replace( "#\(r\)#i"     , "®"  , $txt );
    return $txt;
}
//重新格式化日期
function format_date($date) {
    if (!preg_match('/^\d+$/', $date)) $date = strtotime(trim($date));
    $sec = time() - $date;
    //Sec 1 day is 86400
    if ($sec < 86400) {
        return round($sec/3600). ' hours ago';
    } elseif ($sec < (86400 * 7)) {
        return round($sec/86400). ' days ago';
    } elseif ($sec < (86400 * 7 * 4)) {
        return round($sec/(86400*7)). ' weeks ago';
    } else {
        return date('Y-m-d', $date);
    }
}
?>

PHP 相关文章推荐
php页面消耗内存过大的处理办法
Mar 18 PHP
基于PHP array数组的教程详解
Jun 05 PHP
preg_match_all使用心得分享
Jan 31 PHP
关于js和php对url编码的处理方法
Mar 04 PHP
PHP获取POST数据的几种方法汇总
Mar 03 PHP
php实现随机生成易于记忆的密码
Jun 19 PHP
PHP实现生成唯一会员卡号
Aug 24 PHP
在WordPress的后台中添加顶级菜单和子菜单的函数详解
Jan 11 PHP
Linux环境下php实现给网站截图的方法
May 03 PHP
php微信开发之百度天气预报
Nov 18 PHP
PHP实现json_decode不转义中文的方法
May 20 PHP
PHP仿tp实现mvc框架基本设计思路与实现方法分析
May 23 PHP
粗略计算在线时间,bug:ip相同
Dec 09 #PHP
用PHP函数解决SQL injection
Dec 09 #PHP
php中处理模拟rewrite 效果
Dec 09 #PHP
如何写php程序?
Dec 08 #PHP
IIS下配置Php+Mysql+zend的图文教程
Dec 08 #PHP
从网上搜到的phpwind 0day的代码
Dec 07 #PHP
ajax缓存问题解决途径
Dec 06 #PHP
You might like
php下防止单引号,双引号在接受页面转义的设置方法
2008/09/25 PHP
php连接函数implode与分割explode的深入解析
2013/06/26 PHP
javascript让setInteval里的函数参数中的this指向特定的对象
2010/01/31 Javascript
JQuery Ajax通过Handler访问外部XML数据的代码
2010/06/01 Javascript
简单的JavaScript互斥锁分享
2014/02/02 Javascript
Jquery validation remote 验证的缓存问题解决方法
2014/03/25 Javascript
jQuery中remove()方法用法实例
2014/12/25 Javascript
jQuery团购倒计时特效实现方法
2015/05/07 Javascript
JS实现带提示的星级评分效果完整实例
2015/10/30 Javascript
详解javascript事件绑定使用方法
2016/10/20 Javascript
微信小程序开发之Tabbar实例详解
2017/01/09 Javascript
详解Vue微信授权登录前后端分离较为优雅的解决方案
2018/06/29 Javascript
基于VUE实现判断设备是PC还是移动端
2020/07/03 Javascript
微信小程序 接入腾讯地图的两种写法
2021/01/12 Javascript
[05:31]DOTA2英雄梦之声_第08期_莉娜
2014/06/23 DOTA
在Python的web框架中编写创建日志的程序的教程
2015/04/30 Python
python中私有函数调用方法解密
2016/04/29 Python
Python 实现购物商城,含有用户入口和商家入口的示例
2017/09/15 Python
python2.7读取文件夹下所有文件名称及内容的方法
2018/02/24 Python
python 拷贝特定后缀名文件,并保留原始目录结构的实例
2018/04/27 Python
django使用LDAP验证的方法示例
2018/12/10 Python
Python生成验证码、计算具体日期是一年中的第几天实例代码详解
2019/10/16 Python
python3连接mysql获取ansible动态inventory脚本
2020/01/19 Python
Python字典深浅拷贝与循环方式方法详解
2020/02/09 Python
pycharm中导入模块错误时提示Try to run this command from the system terminal
2020/03/26 Python
django form和field具体方法和属性说明
2020/07/09 Python
美国女鞋品牌:naturalizer(娜然)
2016/08/01 全球购物
办理房产证委托书
2014/09/18 职场文书
群众路线教育实践活动对照检查材料
2014/09/22 职场文书
团代会开幕词
2015/01/28 职场文书
暂停营业通知
2015/04/25 职场文书
不同意离婚上诉状
2015/05/23 职场文书
小学生手册家长意见
2015/06/03 职场文书
新入职员工工作总结
2015/10/15 职场文书
2016年教师师德师风承诺书
2016/03/25 职场文书
Java面试题冲刺第十七天--基础篇3
2021/08/07 面试题