form1.cn
Make a little progress every day

常用PHP函数、语句、SQL等整理

16th of February 2017 PHP PHPcode 2078

查询一个表中字段重复情况,subject为查询的重复字段,as count为重复次数

select `subject`,count(*) as count from wl_dpdf group by `subject` having count>1;


Mysql函数replace替换某字段内容

update course_room set course_ppt=replace(course_ppt,"需要替换的内容","替换为最终需要的内容");


Mysql中FIND_IN_SET的使用

比如我们有一个字段为:userids{23,34,56,76,23},只想查询userids字段中包含”56″这个参数的所有行:

SELECT  FROM `userGroup` WHERE FIND_IN_SET('56',userids);


PHP正则匹配图片地址

$str = "aa<img width='98' src=\"http://127.0.0.1/7/image001.jpg\" style=\"vertical-align:middle;\" />ss";
$pattern="/<[img|IMG].*?src=[\'|\"](.*?(?:[\.gif|\.jpg|\.png]))[\'|\"].*?[\/]?>/";
preg_match_all($pattern,$str,$match);
var_dump($match);


正则替换文本中所有图片,如:给每个图片加宽度

$Ifind['intro'] = preg_replace('/<img(.*?)style=[\"|\'](.*?)[\"|\']\s(.*?)>/i', '<img$1 width="100%" $3>', $Ifind['intro']);


file_put_contents() 函数把一个字符串写入文件中,与依次调用 fopen(),fwrite() 以及 fclose() 功能一样

file_put_contents("./test.txt","data",FILE_APPEND);//FILE_APPEND附加到文件中


str_replace()数组替换

str_replace(array("(", ")", "|"),array("","","-"),$sphinxKey);
str_replace(array("(", ")", "|"),array(""),$sphinxKey);


array_merge 合并一个或多个数组,相同的K会被后数组覆盖

$array1 = array("color" => "red", 2, 4);
$array2 = array("a", "b", "color" => "green", "shape" => "trapezoid", 4);
$result = array_merge($array1, $array2);


js中打印对象

JSON.stringify(this.state)