substr字符串截取
$_SERVER[‘HTTP_HOST’] 获取域名
is_file 判断文件是否存在
$common_function = ROOT_PATH . '/common/function.php';
if(is_file($common_function)){
@include_once $common_function;
}
$_SERVER[‘DOCUMENT_ROOT’] 获取项目的根目录
$data = file_get_contents(“php://input”);
$data = file_get_contents(“php://input”);
php://input 是个可以访问请求的原始数据的只读流。 POST 请求的情况下,最好使用 php://input 来代替 $HTTP_RAW_POST_DATA,因为它不依赖于特定的 php.ini 指令。 而且,这样的情况下 $HTTP_RAW_POST_DATA 默认没有填充, 比激活 always_populate_raw_post_data 潜在需要更少的内存。 enctype=“multipart/form-data” 的时候 php://input 是无效的。
1, php://input 可以读取http entity body中指定长度的值,由Content-Length指定长度,不管是POST方式或者GET方法提交过来的数据。但是,一般GET方法提交数据 时,http request entity body部分都为空。
2,php://input 与
H
T
T
P
R
A
W
P
O
S
T
D
A
T
A
读
取
的
数
据
是
一
样
的
,
都
只
读
取
C
o
n
t
e
n
t
−
T
y
p
e
不
为
m
u
l
t
i
p
a
r
t
/
f
o
r
m
−
d
a
t
a
的
数
据
。
学
习
笔
记
1
,
C
o
e
n
t
e
n
t
−
T
y
p
e
仅
在
取
值
为
a
p
p
l
i
c
a
t
i
o
n
/
x
−
w
w
w
−
d
a
t
a
−
u
r
l
e
n
c
o
d
e
d
和
m
u
l
t
i
p
a
r
t
/
f
o
r
m
−
d
a
t
a
两
种
情
况
下
,
P
H
P
才
会
将
h
t
t
p
请
求
数
据
包
中
相
应
的
数
据
填
入
全
局
变
量
HTTP_RAW_POST_DATA读取的数据是一样的,都只读取Content-Type不为multipart/form-data的数据。 学习笔记 1,Coentent-Type仅在取值为application/x-www-data-urlencoded和multipart/form-data两种情况下,PHP才会将http请求数据包中相应的数据填入全局变量
HTTPRAWPOSTDATA读取的数据是一样的,都只读取Content−Type不为multipart/form−data的数据。学习笔记1,Coentent−Type仅在取值为application/x−www−data−urlencoded和multipart/form−data两种情况下,PHP才会将http请求数据包中相应的数据填入全局变量_POST
2,PHP不能识别的Content-Type类型的时候,会将http请求包中相应的数据填入变量
H
T
T
P
R
A
W
P
O
S
T
D
A
T
A
3
,
只
有
C
o
e
n
t
e
n
t
−
T
y
p
e
为
m
u
l
t
i
p
a
r
t
/
f
o
r
m
−
d
a
t
a
的
时
候
,
P
H
P
不
会
将
h
t
t
p
请
求
数
据
包
中
的
相
应
数
据
填
入
p
h
p
:
/
/
i
n
p
u
t
,
否
则
其
它
情
况
都
会
。
填
入
的
长
度
,
由
C
o
e
n
t
e
n
t
−
L
e
n
g
t
h
指
定
。
4
,
只
有
C
o
n
t
e
n
t
−
T
y
p
e
为
a
p
p
l
i
c
a
t
i
o
n
/
x
−
w
w
w
−
d
a
t
a
−
u
r
l
e
n
c
o
d
e
d
时
,
p
h
p
:
/
/
i
n
p
u
t
数
据
才
跟
HTTP_RAW_POST_DATA 3, 只有Coentent-Type为multipart/form-data的时候,PHP不会将http请求数据包中的相应数据填入php://input,否则其它情况都会。填入的长度,由Coentent-Length指定。 4,只有Content-Type为application/x-www-data-urlencoded时,php://input数据才跟
HTTPRAWPOSTDATA3,只有Coentent−Type为multipart/form−data的时候,PHP不会将http请求数据包中的相应数据填入php://input,否则其它情况都会。填入的长度,由Coentent−Length指定。4,只有Content−Type为application/x−www−data−urlencoded时,php://input数据才跟_POST数据相一致。
5,php://input数据总是跟
H
T
T
P
R
A
W
P
O
S
T
D
A
T
A
相
同
,
但
是
p
h
p
:
/
/
i
n
p
u
t
比
HTTP_RAW_POST_DATA相同,但是php://input比
HTTPRAWPOSTDATA相同,但是php://input比HTTP_RAW_POST_DATA更凑效,且不需要特殊设置php.ini
6,PHP会将PATH字段的query_path部分,填入全局变量$_GET。通常情况下,GET方法提交的http请求,body为空。
例子
1.php用file_get_contents(“php://input”)或者$HTTP_RAW_POST_DATA可以接收xml数据
比如:
getXML.php;//接收XML地址
<?php
$xmldata = file_get_contents("php://input");
$data = (array)simplexml_load_string($xmldata);
?>
这里的$data就是包含xml数据的数组,具体php解析xml数据更新详细的方法
sendXML.php
<?php
$xml = '<xml>xmldata</xml>';//要发送的xml
$url = 'http://localhost/test/getXML.php';//接收XML地址
$header = 'Content-type: text/xml';//定义content-type为xml
$ch = curl_init(); //初始化curl
curl_setopt($ch, CURLOPT_URL, $url);//设置链接
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);//设置是否返回信息
curl_setopt($ch, CURLOPT_HTTPHEADER, $header);//设置HTTP头
curl_setopt($ch, CURLOPT_POST, 1);//设置为POST方式
curl_setopt($ch, CURLOPT_POSTFIELDS, $xml);//POST数据
$response = curl_exec($ch);//接收返回信息
if(curl_errno($ch)){//出错则显示错误信息
print curl_error($ch);
}
curl_close($ch); //关闭curl链接
echo $response;//显示返回信息
?>
2.一个手机上传图片到服务器的小程序
上传文件
<?php
//@file phpinput_post.php
$data=file_get_contents('btn.png');
$http_entity_body = $data;
$http_entity_type = 'application/x-www-form-urlencoded';
$http_entity_length = strlen($http_entity_body);
$host = '127.0.0.1';
$port = 80;
$path = '/p_w_picpath.php';
$fp = fsockopen($host, $port, $error_no, $error_desc, 30);
if ($fp){
fputs($fp, "POST {$path} HTTP/1.1\r\n");
fputs($fp, "Host: {$host}\r\n");
fputs($fp, "Content-Type: {$http_entity_type}\r\n");
fputs($fp, "Content-Length: {$http_entity_length}\r\n");
fputs($fp, "Connection: close\r\n\r\n");
fputs($fp, $http_entity_body . "\r\n\r\n");
while (!feof($fp)) {
$d .= fgets($fp, 4096);
}
fclose($fp);
echo $d;
}
?>
接收文件
<?php
/**
*Recieve p_w_picpath data
**/
error_reporting(E_ALL);
function get_contents() {
$xmlstr= file_get_contents("php://input");
$filename=time().'.png';
if(file_put_contents($filename,$xmlstr)){
echo 'success';
}else{
echo 'failed';
}
}
get_contents();
?>
3.获取HTTP请求原文
/**
* 获取HTTP请求原文
* @return string
*/
function get_http_raw() {
$raw = '';
// (1) 请求行
$raw .= $_SERVER['REQUEST_METHOD'].' '.$_SERVER['REQUEST_URI'].' '.$_SERVER['SERVER_PROTOCOL']."\r\n";
// (2) 请求Headers
foreach($_SERVER as $key => $value) {
if(substr($key, 0, 5) === 'HTTP_') {
$key = substr($key, 5);
$key = str_replace('_', '-', $key);
$raw .= $key.': '.$value."\r\n";
}
}
// (3) 空行
$raw .= "\r\n";
// (4) 请求Body
$raw .= file_get_contents('php://input');
return $raw;
}
version_compare() 用于对比两个「PHP 规范化」的版本数字字符串。 这对于编写仅能兼容某些版本 PHP 的程序很有帮助
<?php
header('content-type:text/html;charset=utf-8');
/**
* 判断php的版本是否在5.3.0以上
*/
echo '<pre>';
//本人使用的版本为 5.2.17
echo 'PHP的当前版本为 '.PHP_VERSION."\n";
var_dump(version_compare(PHP_VERSION,'5.2.0'));
var_dump(version_compare(PHP_VERSION,'5.2.0','='));
var_dump(version_compare(PHP_VERSION,'5.3.0','ge'));
if(version_compare(PHP_VERSION,'5.3.0','ge')){
echo '您的PHP版本大于5.3.0,当前版本为 '.PHP_VERSION;
}else{
echo '您的PHP版本小于5.3.0,当前版本为 '.PHP_VERSION;
}
结果如下:
PHP的当前版本为 5.2.17
int(1)
bool(false)
bool(false)
您的PHP版本小于5.3.0,当前版本为 5.2.17
set_include_path()
下面截图
LIB_PATH
PATH_SEPARATOR
MOD_PATH
array_push() 函数向第一个参数的数组尾部添加一个或多个元素(入栈),然后返回新数组的长度。
<?php
$a=array("red","green");
array_push($a,"blue","yellow");
print_r($a);
?>
Array ( [0] => red [1] => green [2] => blue [3] => yellow )
filter_var() 函数通过指定的过滤器过滤一个变量。
<?php
if(!filter_var("someone@example....com", FILTER_VALIDATE_EMAIL))
{
echo("E-mail is not valid");
}
else
{
echo("E-mail is valid");
}
?>
#结果E-mail is not valid
<?php
if(!filter_var("someone@example.com", FILTER_VALIDATE_EMAIL))
{
echo("E-mail is not valid");
}
else
{
echo("E-mail is valid");
}
?>
#结果E-mail is valid
sprintf() 函数把格式化的字符串写入变量中
<?php
$number = 2;
$str = "Shanghai";
$txt = sprintf("There are %u million cars in %s.",$number,$str);
echo $txt;
?>
## 结果: There are 2 million cars in Shanghai.
http_build_query方法详解(自动拼接生成URL参数字符串)
<?php
$data = array('foo', 'bar', 'cow' => 'milk');
$data_1 = array('k1'=>'foo', 'k2'=>'bar', 'k3' => 'milk');
echo http_build_query($data);
echo "<br>";
echo http_build_query($data, 'hangge_');
echo "<br>";
echo http_build_query($data_1);
?>
结果
0=foo&1=bar&cow=milk
hangge_0=foo&hangge_1=bar&cow=milk
k1=foo&k2=bar&k3=milk
数组
2021-07-09更新
array_column() 函数
$a = array(
array(
'id' => 5698,
'first_name' => 'Bill',
'last_name' => 'Gates',
),
array(
'id' => 4767,
'first_name' => 'Steve',
'last_name' => 'Jobs',
),
array(
'id' => 3809,
'first_name' => 'Mark',
'last_name' => 'Zuckerberg',
)
);
$last_names = array_column($a, 'last_name');
print_r($last_names);
结果
Array
(
[0] => Gates
[1] => Jobs
[2] => Zuckerberg
)
<?php
// 表示由数据库返回的可能记录集的数组
$a = array(
array(
'id' => 5698,
'first_name' => 'Bill',
'last_name' => 'Gates',
),
array(
'id' => 4767,
'first_name' => 'Steve',
'last_name' => 'Jobs',
)
array(
'id' => 3809,
'first_name' => 'Mark',
'last_name' => 'Zuckerberg',
)
);
$last_names = array_column($a, 'last_name', 'id');
print_r($last_names);
?>
Array
(
[5698] => Gates
[4767] => Jobs
[3809] => Zuckerberg
)
2021-07-14更新
array_intersect_key — 使用键名比较计算数组的交集
<?php
$array1 = array('blue' => 1, 'red' => 2, 'green' => 3, 'purple' => 4);
$array2 = array('green' => 5, 'blue' => 6, 'yellow' => 7, 'cyan' => 8);
var_dump(array_intersect_key($array1, $array2));
?>
结果
array(2) {
[“blue”]=>
int(1)
[“green”]=>
int(3)
}
2021-07-15
debug_backtrace() 函数
debug_backtrace() 函数生成 backtrace。
该函数显示由 debug_backtrace() 函数代码生成的数据。
返回一个关联数组。下面是可能返回的元素:
![返回 nothing - 函数调用](https://img-blog.csdnimg.cn/20210715145610169.png?x-oss-process=image/watermark,type_ZmFuZ3poZW5naGVpdGk,shadow_10,text_aHR0cHM6Ly9ibG9nLmNzZG4ubmV0L3h4cHh4cG9vOA==,size_16,color_FFFFFF,t_70)
语法
debug_backtrace()
实例
<?php
function one($str1, $str2)
{
two("Glenn", "Quagmire");
}
function two($str1, $str2)
{
three("Cleveland", "Brown");
}
function three($str1, $str2)
{
print_r(debug_backtrace());
}
one("Peter", "Griffin");
?>
结果
Array
(
[0] => Array
(
[file] => C:webfoldertest.php
[line] => 7
[function] => three
[args] => Array
(
[0] => Cleveland
[1] => Brown
)
)
[1] => Array
(
[file] => C:webfoldertest.php
[line] => 3
[function] => two
[args] => Array
(
[0] => Glenn
[1] => Quagmire
)
)
[2] => Array
(
[file] => C:webfoldertest.php
[line] => 14
[function] => one
[args] => Array
(
[0] => Peter
[1] => Griffin
)
)
)
func_get_args() 获取函数的所有参数
function foo()
{
$numargs = func_num_args(); //参数数量
echo "参数个数是: $numargs<br />\n";
if ($numargs >= 2) {
echo "第二个参数的值:" . func_get_arg(1) . "<br />\n";
}
$arg_list = func_get_args();
for ($i = 0; $i < $numargs; $i++) {
echo "第{$i}个参数值:{$arg_list[$i]}<br />\n";
}
}
foo(1, 'd', 3,4);
输出:
参数个数是: 4
第二个参数的值:d
第0个参数值:1
第1个参数值:d
第2个参数值:3
第3个参数值:4
call_user_func_array 调用回调函数,并把一个数组参数作为回调函数的参数
说明:mixed call_user_func_array ( callable $callback , array $param_arr )
把第一个参数作为回调函数(callback)调用,把参数数组作(param_arr)为回调函数的的参数传入。
返回回调函数的结果。如果出错的话就返回FALSE
1)普通使用:
function a($b, $c) {
echo $b;
echo $c;
}
call_user_func_array('a', array("111", "222"));
//输出 111 222
(2)调用类内部的方法:
Class ClassA {
function bc($b, $c) {
$bc = $b + $c;
echo $bc;
}
}
call_user_func_array(array('ClassA','bc'), array("111", "222"));
//输出 333
(3)支持引用传递:
function a(&$b) {
$b++;
}
$c = 1;
call_user_func_array('a', array(&$c));
echo $c; //输出 2
注意: call_user_func_array 与 call_user_func 这两个函数基本上是类似的,只是在调用上传递参数时存在一些差异。
函数call_user_func_array 传递的第二个参数必须是数组;
函数call_user_func 传递的第二个参数可能是数组,如果是多个参数的话,还是需要以列表的形式列出。
call_user_func ( callback $function [,mixed $parameter [, mixed $...]] )
2021-07-19
array_pop() 删除数组中的最后一个元素
<?php
$a=array("red","green","blue");
array_pop($a);
print_r($a);
?>
Array ( [0] => red [1] => green )
array_values() 函数 array_values() 函数返回包含数组中所有的值的数组。
返回数组中所有的值(不保留键名):
提示:被返回的数组将使用数值键,从 0 开始且以 1 递增。
<?php
$a=array("Name"=>"Peter","Age"=>"41","Country"=>"USA");
print_r(array_values($a));
?>
#结果
Array ( [0] => Peter [1] => 41 [2] => USA )
2021-07-20
current() 函数返回数组中的当前元素(单元)。
每个数组中都有一个内部的指针指向它“当前的”元素,初始指向插入到数组中的第一个元素。
current() 函数返回当前被内部指针指向的数组元素的值,并不移动指针。如果内部指针指向超出了单元列表的末端,current() 返回 FALSE。
<?php
$people = array("Peter", "Joe", "Glenn", "Cleveland");
echo current($people) . "<br>";
?>
如果当前数组是二维数字呢?
<?php
$people = array(
array(
"Peter", "Joe", "Glenn", "Cleveland"
),
array(
"Peter1", "Joe1", "Glenn1", "Cleveland1"
)
);
var_dump(current($people))
?>
运行结果 指向当前二维数组第一个
array(4) {
[0]=>
string(5) "Peter"
[1]=>
string(3) "Joe"
[2]=>
string(5) "Glenn"
[3]=>
string(9) "Cleveland"
}
strip_tags() 函数 ,剥去字符串中的 HTML 标签
<?php
echo strip_tags("Hello <b>world!</b>");
?>
//结果 Hello world!
str_replace() 函数,把字符串 “Hello world!” 中的字符 “world” 替换为 “Shanghai”
<?php
echo str_replace("world","Shanghai","Hello world!");
?>
结果:Hello Shanghai!
自定义函数 ,strFilter()过滤特殊字符
function strFilter($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('$', '', $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('(', '', $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('\\', '', $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(';', '', $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(',', '', $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('、', '', $str);
$str = str_replace('?', '', $str);
$str = str_replace('?', '', $str);
return trim($str);
}
字符串截取
substr($str,0,80) 只支持英文,对多字节字体支持不以后,容易出现乱码
mb_substr($str,0,80,‘utf-8’); 中英文混杂都是支持的,兼容性好。
array_slice
array_slice() 函数在数组中根据条件取出一段值,并返回。
注释:如果数组有字符串键,所返回的数组将保留键名。
1.例子1
<?php
$a=array("red","green","blue","yellow","brown");
print_r(array_slice($a,2));
?>
结果
Array ( [0] => blue [1] => yellow [2] => brown )
2.例子2
<?php
$a=array("red","green","blue","yellow","brown");
print_r(array_slice($a,1,2));
?>
Array ( [0] => green [1] => blue )
3.例子3
<?php
$a=array("red","green","blue","yellow","brown");
print_r(array_slice($a,-2,1));
?>
结果
Array ( [0] => yellow )
4.例子4
<?php
$a=array("red","green","blue","yellow","brown");
print_r(array_slice($a,1,2,true));
?>
结果
Array ( [1] => green [2] => blue )
5.例子5
<?php
$a=array("a"=>"red","b"=>"green","c"=>"blue","d"=>"yellow","e"=>"brown");
print_r(array_slice($a,1,2));
$a=array("0"=>"red","1"=>"green","2"=>"blue","3"=>"yellow","4"=>"brown");
print_r(array_slice($a,1,2));
?>
结果
Array ( [b] => green [c] => blue )
Array ( [0] => green [1] => blue )