分享web开发知识

注册/登录|最近发布|今日推荐

主页 IT知识网页技术软件开发前端开发代码编程运营维护技术分享教程案例
当前位置:首页 > 软件开发

PHP 随笔

发布时间:2023-09-06 02:20责任编辑:胡小海关键词:PHP
//输出一段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" >‘;
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("ublock-view paragraph-view" data-yne-c>
去掉重复
M("Article")->where("catid=12andquyu!=‘".NULL."‘andquyu!=‘‘")->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);
//截取中文字符无乱码函数
functionCHsubstr($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;
}
}
//加密会员登录的密码:
/**
*加密会员登录的密码。
*@paramint$username账号
*@paramstring$password登陆密码
*@returnstring加密后的密码
*@seelogin,password
*/
functioncrypt($username,$password){
$md5=pack(‘H*‘,md5($username.‘@‘.$password.‘.Z‘));
returnpreg_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" value="{$keyword}" placeholder="请输入关键字...">
<button >搜索</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);
}
//ThinkPHP where查询条件//https://www.cnblogs.com/jingmin/p/6407144.html
$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=4orstatus=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);
$where[‘ono‘]=array(‘in‘,$cc);
//获取字段第一个字首字母
$val[‘letter‘]=$this->getfirstchar($_POST[‘user‘]);//使用(获取传过来姓名的首字母大写)
functiongetfirstchar($s0){
$fchar=ord($s0{0});
if($fchar>=ord("A")and$fchar<=ord("z"))returnstrtoupper($s0{0});
$s1=iconv("UTF-8","gb2312",$s0);
$s2=iconv("gb2312","UTF-8",$s1);
if($s2==$s0){$s=$s1;}else{$s=$s0;}
$asc=ord($s{0})*256
我的编程学习网——分享web前端后端开发技术知识。 垃圾信息处理邮箱 tousu563@163.com 网站地图
icp备案号 闽ICP备2023006418号-8 不良信息举报平台 互联网安全管理备案 Copyright 2023 www.wodecom.cn All Rights Reserved