当前位置: 移动技术网 > IT编程>开发语言>PHP > PHP 随笔

PHP 随笔

2018年10月31日  | 移动技术网IT编程  | 我要评论
//输出一段utf-8编码的html
$this->show('<p>欢迎使用 <b>thinkphp</b>!</p>','utf-8');
字符串替换$br=str_replace("/>", "/><br />", $con);
php把文本框回车转换成html换行
$aa=@ereg_replace("\r\n"," ",$_post['words']);//php把文本框回车转换成html换行
$bb=explode(' ',$aa);//字符串转为数组
$words=array_unique($bb);//数组去重复
$words=implode(' ',$words);//数组转为字符串
 $data['dic'] =$words;
//file_put_contents — 将一个字符串写入文件 日志
/* 
$file  文件名 
$person  写入的内容  
file_append 如果文件
$file
已经存在,追加数据而不是覆盖。
*/
file_put_contents('log.log', $person, file_append );
 
//正则替换中文文字
$string = "中文123高深abc开心。?我们";
echo preg_replace('#(?:(?![,。?])[\xc0-\xff][\x80-\xbf]+)+#','<b>$0</b>',$string);
//<b>中文</b>123<b>高深</b>abc<b>开心</b>。?<b>我们</b>
//正则替换数字
echo preg_replace('#(\d)+#','<b>$0</b>',$string);
//中文<b>123</b>高深abc开心。?我们
//(?:[\xc0-\xff][\x80-\xbf]+) 单个中文字符,不需要引用,因此使用?:
//(?![,。?]) 排除中文标点符号,这里要写入中文标点
//(?:(?![,。?])[\xc0-\xff][\x80-\xbf]+) 排除中文标点符号后的中文字符
//(?:[\xc0-\xff][\x80-\xbf]+)+ 1个以上的中文字符
//去掉style行内样式
$str='<img src="images/logo.png" style="width:100px;height:10px" alt="">';
echo preg_replace('/style=\".*?\"/',' ',$str);
 
html代码过滤并截取:$ser[$i]['description']= $this->chsubstr(strip_tags($ser[$i]['description']),0,200);
字符串长度:strlen($string);
数字满三位添加一逗号:$proe[$i]['s_money']= number_format($proe[$i]['s_money']);
去掉重复(不统计):
$shopquan=m("quan")->group("s_id")->limit(0,6)->order("x_date desc")->select();
去掉重复(统计):
$count=m("record")->where("uid=".$_session['uid'])->count('distinct pid');
去掉重复
m("article")->where("catid=12 and quyu !='".null."' and quyu!=''")->field("quyu")->distinct(true)->select();
 
截取字符串:mb_substr(字符串,开始,长度,utf8/gb2312);
查找单词初始位置:
strstr//搜索字符串在另一字符串中的首次出现从匹配点返回字符串的其余部分(对大小写敏感)未找到则返回 false
stristr("hello world!","world");//查找字符串在另一字符串中第一次出现的位置(大小写不敏感)
//返回字符串在另一字符串中首次出现的位置(对大小写敏感)//如未找到则返回 false
strpos //返回字符串在另一字符串中第一次出现的位置(大小写不敏感)
stripos //返回字符串在另一字符串中第一次出现的位置(大小写不敏感)
获取文件后缀名:pathinfo($picurl, pathinfo_extension)
error_reporting(0);//禁止显示php警告提示
define("pi",3.14);//定义常量
defined($string)//函数可以帮助我们判断一个常量是否已经定义
constant()//动态的输出不同的常量
//php自定函数截取字符串
$waihui_val = m("article")->where("catid=3")->order("inputtime desc")->limit("0,10")->select();
  for($i=0;$i<count($waihui_val);$i++){
   $waihui_val[$i]['title']= $this->chsubstr($waihui_val[$i]['title'],0,44);
  }
  $this->assign("waihui_val",$waihui_val);//dump($waihui_val);
//截取中文字符无乱码函数 
    function chsubstr($string, $start, $length){
        if(strlen($string)>$length){
            $str='';
            $len=$start+$length;
            $i = $start;
            while($i<$len){
                if(ord(substr($string, $i, 1))>=128){
                    $str.=substr($string, $i, 3);
                    $i = $i+ 3;
                }else{
                    $str.=substr($string, $i, 1);
                    $i ++;
                }
            }
            $string=$str."...";
            return $string;
        }else{
            return $string;
        }
    }
//加密会员登录的密码:
/**
 * 加密会员登录的密码。
 * @param int $username 账号
 * @param string $password 登陆密码
 * @return string 加密后的密码
 * @see login, password
 */
function crypt($username, $password) {
    $md5 = pack('h*', md5($username . '@' . $password . '.z'));
    return preg_replace('/=+$/', '', base64_encode($md5));
}
//处理传值为空
$id=!empty($_get['id'])?$_get['id']:147;
//传id,参数
{:u('home/decoration/info',array('id'=>$val['id']))}
//匹配关键字
html:
<input type="text" name="keyword" id="keyword"  value="{$keyword}" placeholder="请输入关键字..."> 
<button id="sub_mit">搜索</button>
js:
 <script>
    $(document).ready(function (){
     $("#sub_mit").click(function (){
     
      var keyword    = $("#keyword").val();
      window.location.href="__app__/admin/article/index?keyword="+keyword;
     });
    });
   </script>
php:
$keyword = trim($_get['keyword']);//i('post.keyword');
 if (!empty($keyword)) {
   $sql_sql="select * from `news` where `keywords` like '%{$keyword}%' order by date desc";
    $this->assign("keyword",$keyword);
  }
$where ['is_open|is_hot'] =1;
$where ['title'] = array('like','%{$keys}%');
$where ['status'] = array(array('gt',1),array('lt',9));
$where ['id'] = array(array('gt',3),array('lt',10), 'or') ;
$where['title|desc'] =array('like',array('%'.$key.'%'),'or');
//'_multi'=>true数组的最后,多条件匹配
$where['status&title'] =array('1','thinkphp','_multi'=>true);
$aa=explode(',',$list[$i]['ids']);
$bb['id']=array('in',$aa);
$orderinfo=m("car")->where($bb)->select();
//一、查询多条
$psid=m("category")->where("parentid=28")->field("id")->select();
if(!empty($psid)){
       for($i=0;$i<count($psid);$i++){
           $aa.=$psid[$i]['id'].",";
       }
     $aa=mb_substr($aa,0,strlen($aa)-1,'utf-8');
     $where['catid']=array('in',$aa);
 }
//二、查询多条
$orderno=m("order")->where("status=4 or status=5")->field("ono")->select();
        for($i=0;$i<count($orderno);$i++){
            $bb.=$orderno[$i]['ono'].",";
        }
        $bb=substr($bb,0,strlen($bb)-1);
        $cc=explode(',',$bb);

如对本文有疑问, 点击进行留言回复!!

相关文章:

验证码:
移动技术网