学无止境详情

PHP Web开发之常用方法

发表:2015-09-05    编辑:南昌开优网络    浏览:4464    
标签:PHP应用

strip_tags($str) 去掉 HTML 及 PHP 的标记。本函式和 fgetss() 有着相同的功能 
PHP清除html、css、js格式并去除空格的PHP函数 
function cutstr_html($string, $sublen)    
 {
  $string = strip_tags($string);
  $string = preg_replace ('/\n/is', '', $string);
  $string = preg_replace ('/ | /is', '', $string);
  $string = preg_replace ('/ /is', '', $string);
  
  preg_match_all("/[\x01-\x7f]|[\xc2-\xdf][\x80-\xbf]|\xe0[\xa0-\xbf][\x80-\xbf]|[\xe1-\xef][\x80-\xbf][\x80-\xbf]|\xf0[\x90-\xbf][\x80-\xbf][\x80-\xbf]|[\xf1-\xf7][\x80-\xbf][\x80-\xbf][\x80-\xbf]/", $string, $t_string);   
  if(count($t_string[0]) - 0 > $sublen) $string = join('', array_slice($t_string[0], 0, $sublen))."…";   
  else $string = join('', array_slice($t_string[0], 0, $sublen));
  
  return $string;
 }
 
htmlspecialchars 将特殊字元转成 HTML 格式
htmlentities 将所有的字元都转成 HTML 字串 

PHP截取字符串的函数代码
function utf_substr($str,$len)
{
for($i=0;$i<$len;$i++)
{
$temp_str=substr($str,0,1);
if(ord($temp_str) > 127)
{
$i++;
if($i<$len)
{
$new_str[]=substr($str,0,3);
$str=substr($str,3);
}
}
else
{
$new_str[]=substr($str,0,1);
$str=substr($str,1);
}
}
return join($new_str)."...";
}


//php sql防注入代码
function GetSafeSQL($str)
{
   $str = str_replace("and","",$str);
   $str = str_replace("execute","",$str);
   $str = str_replace("update","",$str);
   $str = str_replace("count","",$str);
   $str = str_replace("chr","",$str);
   $str = str_replace("mid","",$str);
   $str = str_replace("master","",$str);
   $str = str_replace("truncate","",$str);
   $str = str_replace("char","",$str);
   $str = str_replace("declare","",$str);
   $str = str_replace("select","",$str);
   $str = str_replace("create","",$str);
   $str = str_replace("delete","",$str);
   $str = str_replace("insert","",$str);
   $str = str_replace("'","",$str);
   $str = str_replace("%","",$str);
   $str = str_replace("&","",$str);
   $str = str_replace("--","",$str);
   $str = str_replace("<","",$str);
   $str = str_replace(">","",$str);
   $str = str_replace(" ","",$str);
   $str = str_replace("or","",$str);
   $str = str_replace("=","",$str);
   $str = str_replace(";","",$str);   
   return $str;
}



// 去掉过滤html标签
//--------------------------
//--------------------------
function GetNoHTMLString($content)
{
$content = preg_replace("/<a[^>]*>/i", "", $content);   
$content = preg_replace("/<\/a>/i", "", $content); 
$content = preg_replace("/<p[^>]*>/i", "", $content);   
$content = preg_replace("/<\/p>/i", "", $content);   
$content = preg_replace("/<span[^>]*>/i", "", $content);   
$content = preg_replace("/<\/span>/i", "", $content);  
$content = preg_replace("/<html[^>]*>/i", "", $content);   
$content = preg_replace("/<\/html>/i", "", $content);  
$content = preg_replace("/<div[^>]*>/i", "", $content);   
$content = preg_replace("/<\/div>/i", "", $content);   
$content = preg_replace("/<img[^>]*>/i", "", $content); 
$content = preg_replace("/<script[^>]*>/i", "", $content); 
$content = preg_replace("/<\/script>/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);//去除样式       
$content = preg_replace("/width=.+?['|\"]/i",'',$content);//去除样式    
$content = preg_replace("/height=.+?['|\"]/i",'',$content);//去除样式    
$content = preg_replace("/border=.+?['|\"]/i",'',$content);//去除样式    
$content = preg_replace("/face=.+?['|\"]/i",'',$content);//去除样式
$content = preg_replace("/face=.+?['|\"]/",'',$content);//去除样式 只允许小写 正则匹配没有带 i 参数  
return $content;
}

//对字符串进行HTML过滤显示
function _html($_string) {
if (is_array($_string)) {
foreach ($_string as $_key => $_value) {
$_string[$_key] = _html($_value);   //这里采用了递归,如果不理解,那么还是用htmlspecialchars
}
} else {
$_string = htmlspecialchars($_string);
}
return $_string;
}

隐藏IP最后一位
function hideIP($ip) {
        $res = strrpos($ip,'.');
        $tmp = substr($ip,0,$res);
        return $tmp.".*";
    }
//mysql按时间查询
SQL语句: Select * From user Where DATE_FORMAT(birthday,'%m-%d') >= '06-03' and DATE_FORMAT(birthday,'%m-%d') <= '07-08';
%W 星期名字(Sunday……Saturday)   
%D 有英语前缀的月份的日期(1st, 2nd, 3rd, 等等。)   
%Y 年, 数字, 4 位   
%y 年, 数字, 2 位   
%a 缩写的星期名字(Sun……Sat)   
%d 月份中的天数, 数字(00……31)   
%e 月份中的天数, 数字(0……31)   
%m 月, 数字(01……12)   
%c 月, 数字(1……12)   
%b 缩写的月份名字(Jan……Dec)   
%j 一年中的天数(001……366)   
%H 小时(00……23)   
%k 小时(0……23)   
%h 小时(01……12)   
%I 小时(01……12)   
%l 小时(1……12)   
%i 分钟, 数字(00……59)   
%r 时间,12 小时(hh:mm:ss [AP]M)   
%T 时间,24 小时(hh:mm:ss)   
%S 秒(00……59)   
%s 秒(00……59)   
%p AM或PM   
%w 一个星期中的天数(0=Sunday ……6=Saturday )   
%U 星期(0……52), 这里星期天是星期的第一天   
%u 星期(0……52), 这里星期一是星期的第一天   
%% 一个文字“%”。 
php格式化mysql时间值
<?php echo date('Y-m-d',strtotime($rows['createdate'])); ?>
分享
  1. 上一篇:PHP Web开发之静态方法
  2. 下一篇:PHP Web开发之操作XML