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 相关文章推荐
BBS(php &amp; mysql)完整版(八)
Oct 09 PHP
php empty,isset,is_null判断比较(差异与异同)
Oct 19 PHP
php更新mysql后获取影响的行数发生异常解决方法
Mar 28 PHP
简单的php数据库操作类代码(增,删,改,查)
Apr 08 PHP
安装apache2.2.22配置php5.4(具体操作步骤)
Jun 26 PHP
Codeigniter中mkdir创建目录遇到权限问题和解决方法
Jul 25 PHP
详解WordPress中简码格式标签编写的基本方法
Dec 22 PHP
比较完整的微信开发php代码
Aug 02 PHP
yii框架redis结合php实现秒杀效果(实例代码)
Oct 26 PHP
laravel框架中控制器的创建和使用方法分析
Nov 23 PHP
php设计模式之原型模式分析【星际争霸游戏案例】
Mar 23 PHP
Thinkphp 框架扩展之Widget扩展实现方法分析
Apr 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
《忧国的莫里亚蒂》先导宣传图与STAFF公开
2020/03/04 日漫
php桌面中心(一) 创建数据库
2007/03/11 PHP
让php处理图片变得简单 基于gb库的图片处理类附实例代码下载
2011/05/17 PHP
基于jquery的跨域调用文件
2010/11/19 Javascript
有关于JS辅助函数inherit()的问题
2013/04/07 Javascript
文本框中禁止非数字字符输入比如手机号码、邮编
2013/08/19 Javascript
jquery获取子节点和父节点的示例代码
2013/09/10 Javascript
Windows系统中安装nodejs图文教程
2015/02/28 NodeJs
Jquery1.9.1源码分析系列(六)延时对象应用之jQuery.ready
2015/11/24 Javascript
开启BootStrap学习之旅
2016/05/04 Javascript
Javascript将双字节字符转换成单字节字符并计算长度
2016/06/22 Javascript
Javascript基础学习笔记(菜鸟必看篇)
2016/07/22 Javascript
基于JavaScript实现树形下拉框
2016/08/10 Javascript
探究Vue.js 2.0新增的虚拟DOM
2016/10/20 Javascript
node.js缺少mysql模块运行报错的解决方法
2016/11/13 Javascript
JQuery实现图片轮播效果
2017/05/08 jQuery
bootstrap基本配置_动力节点Java学院整理
2017/07/14 Javascript
vue给input file绑定函数获取当前上传的对象完美实现方法
2017/12/15 Javascript
微信小程序MUI侧滑导航菜单示例(Popup弹出式,左侧不动,右侧滑动)
2019/01/23 Javascript
Node.js Windows Binary二进制文件安装方法
2019/05/16 Javascript
解决layui追加或者动态修改的表单元素“没效果”的问题
2019/09/18 Javascript
使用Vant完成DatetimePicker 日期的选择器操作
2020/11/12 Javascript
python 脚本生成随机 字母 + 数字密码功能
2018/05/26 Python
python生成以及打开json、csv和txt文件的实例
2018/11/16 Python
anaconda3安装及jupyter环境配置全教程
2020/08/24 Python
英国最大的奢侈品零售网络商城:Flannels
2016/09/16 全球购物
超市营业员求职简历的自我评价
2013/10/17 职场文书
简短的公司员工自我评价分享
2013/11/13 职场文书
参观监狱心得体会
2014/01/02 职场文书
电子商务助理求职自荐信
2014/04/10 职场文书
工程学毕业生自荐信
2014/06/14 职场文书
2014年党务工作总结
2014/11/25 职场文书
辩护词格式
2015/05/22 职场文书
初中团委工作总结
2015/08/13 职场文书
大学生如何逃脱“毕业季创业队即散伙”魔咒?
2019/08/19 职场文书
Golang 空map和未初始化map的注意事项说明
2021/04/29 Golang