在编
写代码的时候有个神奇的汇总是好的!下面这里收集了 10+ PHP 代码片段,可以帮助你开发 PHP 项目。这些 PHP 片段对于 PHP 初学者也非常有帮助,非常容易学习,让我们开始学习吧~
10个 ThinkPHP 开发常用代码片段
一.php中截取中文字符串
<?php
< ?php
//截取中文字符串
function mysubstr($str, $start, $len) {
$tmpstr = "";
$strlen = $start + $len;
for($i = 0; $i < $strlen; $i++) {
if(ord(substr($str, $i, 1)) > 0xa0) {
$tmpstr .= substr($str, $i, 2);
$i++;
} else
$tmpstr .= substr($str, $i, 1);
}
return $tmpstr;
}
?>
2. 截取utf8
编码的多字节字符串
1
2
3
4
5
6
7
8
9
10
<?php
< ?php
//截取utf8字符串
function utf8Substr($str, $from, $len)
{
return preg_replace('#^(?:[\\x00-\\x7F]|[\\xC0-\\xFF][\\x80-\\xBF]+){0,'.$from.'}'.
'((?:[\\x00-\\x7F]|[\\xC0-\\xFF][\\x80-\\xBF]+){0,'.$len.'}).*#s',
'$1',$str);
}
?>
二.php curl通过代理获取数据
$curl=curl_init();
curl_setopt($curl, CURLOPT_URL, "http://www.baidu.com/");
curl_setopt($curl, CURLOPT_USERAGENT, '
Mozilla/5.0 (X11; Ubuntu; Linux i686; rv:20.0) Gecko/20100101 Firefox/20.0');
curl_setopt($curl, CURLOPT_REFERER, 'http://www.guahao.com/');
curl_setopt($curl, CURLOPT_POST, true);
curl_setopt($curl, CURLOPT_POSTFIELDS, http_build_query($param));
curl_setopt($curl, CURLOPT_RETURNTRANSFER, true);
curl_setopt($curl, CURLOPT_
HTTPPROXYTUNNEL, true);
curl_setopt($curl, CURLOPT_PROXYAUTH, CURLAUTH_
BASIC);
curl_setopt($curl, CURLOPT_PROXYTYPE, CURLPROXY_SOCKS5);
curl_setopt($curl, CURLOPT_PROXY, "180.186.11.121");
curl_setopt($curl, CURLOPT_PROXYPORT, "37211");
curl_setopt($curl, CURLOPT_PROXYUSERPWD, "taras:taras-ss5");
$result=curl_exec($curl);
三.php获取中文字符串的首字符拼音字母
$str="这是一个测试程序1";
echo getFirstCharCode($str);
function getFirstCharCode($str){
$str=
iconv("UTF-8","gb2312", $str);
$targetChar='*';
$i=0;
while($i<strlen($str) ) {
$tmp=bin2hex(substr($str,$i,1));
if($tmp>='B0'){ //汉字的开始
$t=getLetter(hexdec(bin2hex(substr($str,$i,2))));
$targetChar=$t==-1 ? '*' : $t ;
//printf("%c",$t==-1 ? '*' : $t );
break;
//$i+=2;
}
else{
$targetChar=substr($str,$i,1);
break;
//printf("%s",substr($str,$i,1));
//$i++;
}
}
if(is_numeric($targetChar)){
return chr($targetChar);
}
else{
return $targetChar;
}
}
10个 ThinkPHP 开发常用代码片段
四.PHP使用curl伪造IP地址和
header信息
10个 ThinkPHP 开发常用代码片段
curl虽然功能强大,但是只能伪造$_SERVER["HTTP_X_FORWARDED_FOR"],对于大多数IP地址检测程序来说,$_SERVER["REMOTE_ADDR"]很难被伪造:首先是client.php的代码
10个 ThinkPHP 开发常用代码片段
然后是server.php
10个 ThinkPHP 开发常用代码片段
五.sphinx 增量索引实现实例
10个 ThinkPHP 开发常用代码片段
六.curl 添加cookie伪造登陆
抓取数据
10个 ThinkPHP 开发常用代码片段
<?php
header("Content-type:text/html;Charset=utf8");
$ch =curl_init();
curl_setopt($ch,CURLOPT_URL,'http://www.babytree.com/user/picjournal.php');
$header = array();
//curl_setopt($ch,CURLOPT_POST,true);
//curl_setopt($ch, CURLOPT_POSTFIELDS, $fields);
curl_setopt($ch,CURLOPT_RETURNTRANSFER,true);
curl_setopt($ch,CURLOPT_HEADER,true);
curl_setopt($ch,CURLOPT_HTTPHEADER,$header);
curl_setopt($ch,CURLOPT_COOKIE,'B=115.100.62.7.1401937092035530; bdshare_firstime=1401937092199; __myutma=122328856.1548793539.1401937093.1408503164.1408694138.69;');
$content = curl_exec($ch);
echo "<pre>";print_r(curl_error($ch));echo "</pre>";
echo "<pre>";print_r(curl_getinfo($ch));echo "</pre>";
echo "<pre>";print_r($header);echo "</pre>";
echo "</br>",$content;
七.php curl 上传文件
//如果php文件是utf8编码,系统是GBK编码,那么就需要转下编码,要不然Php在系统中找不到这个文件
$file = realpath(mb_convert_encoding('测试图片.JPG','GBK','utf8'));
$file = realpath('temp.jpg'); //要上传的文件
$fields['f'] = '@'.$file; // 前面加@符表示上传图片
$ch =curl_init();
curl_setopt($ch,CURLOPT_URL,'http://localhost/upload.php');
curl_setopt($ch,CURLOPT_POST,true);
curl_setopt($ch, CURLOPT_POSTFIELDS, $fields);
curl_setopt($ch,CURLOPT_RETURNTRANSFER,true);
$content = curl_exec($ch);
echo $content;
八.php+shell检测文件类型
function checkFileType($filename){
//文件头
$_typecode = array(
'3780',//PDF
'8075',//.docx,.xlsx,.
pptx,.potx,.vsdx,.odt
'208207',//.doc,.xls,.ppt,.vsd,.pot,.wps,.dps,.et
);
$file = fopen($filename, "rb");
//contents = stream_get_contents($file);
//$contents =
fread($file,
filesize($filename));
$bin = fread($file, 2); //只读2字节
fclose($file);
$strInfo = @unpack("C2chars", $bin);// C为无符号整数,网上搜到的都是c,为有符号整数,这样会产生负数判断不正常
$typeCode = intval($strInfo['chars1'].$strInfo['chars2']);
exec("file $filename",$output,$return_var);//用linux系统命令file判断上传文件的类型,主要是判断txt,rtf文件类型
$pattern = '/text,/';//rtf和txt文档用file检测都会被检测为text
$_count = preg_match($pattern,strrchr($output[0],":"));
echo $typeCode;
if(in_array($typeCode,$_typecode) || $_count == 1) {
return true;
}else{
return false;
}
}
九.获取用户真实的IP
function getRealIpAddr()
{
if (!emptyempty($_SERVER['HTTP_CLIENT_IP']))
{
$ip=$_SERVER['HTTP_CLIENT_IP'];
}
elseif (!emptyempty($_SERVER['HTTP_X_FORWARDED_FOR']))
//to check ip is pass from proxy
{
$ip=$_SERVER['HTTP_X_FORWARDED_FOR'];
}
else
{
$ip=$_SERVER['REMOTE_ADDR'];
}
return $ip;
}
十.禁止特定的IP访问你的网站
if ( !file_exists('blocked_ips.txt') ) {
$deny_ips = array(
'127.0.0.1',
'192.168.1.1',
'83.76.27.9',
'192.168.1.163'
);
} else {
$deny_ips = file('blocked_ips.txt');
}
// read user ip adress:
$ip = isset($_SERVER['REMOTE_ADDR']) ? trim($_SERVER['REMOTE_ADDR']) : '';
// search current IP in $deny_ips array
if ( (array_search($ip, $deny_ips))!== FALSE ) {
// address is blocked:
echo 'Your IP adress ('.$ip.') was blocked!';
exit;
}