php函数笔记

作者:matrix 发布时间:2013年11月3日 分类:零零星星

图片4788-php函数笔记

TIME: 20190120 工作几年回过头来发现最近真理这篇post都是17年4月份了。看来工作之后真是进步或许没时间亦或变懒,不为简单函数做笔记。
大多都外链工具所用到的,整理整理,以便后用。

file_get_contents(函数把整个文件读入一个字符串中。

 htmlspecialchars() 函数将字符转换为 HTML 实体。

isset($ab)判断$ab是否存在

error_reporting(0);//禁用错误报告

使用@符号也可以

explode("/",$str)把字符串以/分割为数组

preg_match_all ()用正则匹配字符串并保存到数组中

preg_match_all ("/share_uk=(.*?)share/",$ukidConnect,$ukidList, PREG_PATTERN_ORDER);

//使用preg_match_all正则匹配数据并保存到$titleList数组中

$ukidList[0][1]表示第1数组【第一个括弧的值,如.*?】匹配到的字符串

如果preg_match_all换成preg_match,$ukidList[1]也表示第1数组【第一个括弧的值,如.*?】匹配到的字符串

废弃的eregi函数同理,传说使用preg函数更高效

strstr() 函数搜索一个字符串在另一个字符串中的第一次出现。

strpos() 返回字符串在另一个字符串中第一次出现的位置。

strpos 区别大小写strpos查找成功后则是返回的是位置。因为位置有可能是0,所以判断查找失败使用===false更合适。

substr_count()函数计算子串在字符串中出现的次数。

strlen() 函数返回字符串的长度。

 substr() 取固定长度的字符串

$zhuaqian=strpos($titleList[0][0],"title>");
$zhuahou=strpos($titleList[0][0],"_免费高速下载|百度云 网盘-分享无限制");
$xinzhi=substr($titleList[0][0],6,$zhuahou-6); //取适当的两个位置中间的值也就是文件名

$_SERVER["REQUEST_URI"];  php自带的函数

取得当前url的页面url。

http://127.0.0.1/xxx.php?st=http://hhtjim.com

返回:/xxx.php?st=http://hhtjim.com

$NOWstr =  'http://' .$_SERVER['HTTP_HOST'].$_SERVER["REQUEST_URI"];//由于$_GET['url']取url不完整
$NEWstr=explode('?url=',$NOWstr);
$quurl = $NEWstr[1];

curl()函数

作用大大的有。

示例代码:


function curl_get($url,$post=false,$carry_ua=true){ $ch=curl_init($url); if($carry_ua){ curl_setopt($ch, CURLOPT_HTTPHEADER, array('User-Agent:Mozilla/5.0 (iPhone; U; CPU iPhone OS 3_1_2 like Mac OS X; en-us) AppleWebKit/528.18 (KHTML, like Gecko) Version/4.0 Mobile/7D11 Safari/528.16')); } if ($post) { curl_setopt($ch, CURLOPT_POSTFIELDS, $post); } curl_setopt($ch, CURLOPT_RETURNTRANSFER, true); $get_url = curl_exec($ch); curl_close($ch); return $get_url; }

不懂,再保留另外的

$curlPost=’http://songtaste.com/time.php’; 
$ch = curl_init();//初始化curl    
curl_setopt($ch,CURLOPT_URL, $url);//提交到指定网页    
curl_setopt($ch, CURLOPT_HEADER, 0);//设置header    
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);//要求结果为字符串且输出到屏幕上    
curl_setopt($ch, CURLOPT_POST, 1);//post提交方式    
curl_setopt($ch, CURLOPT_POSTFIELDS, $curlPost);
$data = curl_exec($ch);//运行curl    
curl_close($ch);
print_r($data);//输出结果  

模拟POST提交数据

 $post_data = array('nid' => '13714766852756201', 'shorturl' => 'QeEWZNzPansuQ');
$url='http://ajy8hpcasy.l17.yunpan.cn/share/downloadfile/';
$_REFERER='http://ajy8hpcasy.l17.yunpan.cn/lk/QeEWZNzPansuQ';
$ch = curl_init();
curl_setopt($ch, CURLOPT_POST, 1);
curl_setopt($ch, CURLOPT_URL,$url);
curl_setopt($ch, CURLOPT_POSTFIELDS, $post_data);
curl_setopt($ch, CURLOPT_REFERER,$_REFERER);
ob_start();
curl_exec($ch);
$result = ob_get_contents() ;
ob_end_clean(); 
echo $result;

set_time_limit(0);//为0时:允许脚本运行的时间无上限。不设置此函数的话默认为30s

str_replace()使用一个字符串替换字符串中的另一些字符。

header()向客户端浏览器发送 HTTP 。

header("location: http://hhtjim.com");//跳转到http://hhtjim.com

list()  函数用数组中的元素为一组变量赋值。实际上是一种语言结构,不是函数。

array_slice() 函数在数组中根据条件取出一段值,并返回。

list($shareid, $uk) = array_slice($res, 1, 2);

//数组中有shareid, uk元素,让元素名最为变量名字。

//$res为总的数组,1为规定取出元素的开始位置。2为需要返回的数组数量。

define() 函数定义一个常量

define("GREETING","Hello world!");

//常量名为GREETING,值为"Hello world!

php中?(问号)和:(冒号)的作用:精简的if语句

条件表达式e1?e2:e3,若e1为真(非0),则此表达式的值为e2的值;若为假,则表达式的值为e3

例如:

preg_match('|Location: (.+)\r|', $src, $res);
$songurl = $res ? $res[1] : exit(" error!");

implode() 函数把数组元素组合为一个字符串。

json_decode()函数:接受一个 JSON 格式的字符串并且把它转换为 PHP 变量

示例:

$rt='{"errno":0,"errmsg":"\u64cd\u4f5c\u6210\u529f","data":{"downloadurl":"http:\/\/wsdl25.yunpan.cn\/share.php?method=Share.download&fhash=119f736a936d62396d7dd71ff8fb7d187d69d3d9&xqid=23620387&fname=%E8%AF%B4%E6%98%8E.doc&fsize=211968&nid=13754589643979953&cqid=157ddad976b9aa811591ed43134891f7&st=48ea3770079bce5360f952cff1dc9d29&e=1382784599&dt=25.1291649b2f9ed61380291c5c0d711ca9","params":[]}}';
$obj = json_decode($rt);
$dll=$obj->data->downloadurl;
echo $dll;

结果显示:http://wsdl25.yunpan.cn/share.php?method=Share.download&fhash=119f736a936d62396d7dd71ff8fb7d187d69d3d9&xqid=23620387&fname=%E8%AF%B4%E6%98%8E.doc&fsize=211968&nid=13754589643979953&cqid=157ddad976b9aa811591ed43134891f7&st=48ea3770079bce5360f952cff1dc9d29&e=1382784599&dt=25.1291649b2f9ed61380291c5c0d711ca9

示例二:

json数据:

{
"songs": [{
"score": 100,
"copyrightId": 0,
"mvid": 0,
"mp3Url": "http://m1.music.126.net/fG44YGce-id8VZ9LjQIPpQ==/1069824813834035.mp3"
}],
"code": 200
}

php处理:

$eo='上面的json数据';
$obj=json_decode($eo,true);
echo $obj['songs'][0]['mp3Url'];

结果返回:
h t t p : / /m1.music.126.net/fG44YGce-id8VZ9LjQIPpQ==/1069824813834035.mp3

get_headers()函数获取服务器响应 HTTP 请求所发送的所有标头

parse_str() 函数把URL参数解析到到变量中。

 array_unique() 函数移除数组中的重复的值

$b = &$a; //变量前&  表示$b 和 $a 引用了同一个变量

parse_url()解析 URL并返回组成部分为数组内容

strip_tags() 去除 HTML、XML 以及 PHP 的标签。

extract() 函数 把数组中的键名设置为变量,其键值为变量的值。

对于数组中的每个元素,键名用于变量名,键值用于变量值。

substr_replace() 函数把字符串的一部分替换为另一个字符串。

substr_replace($P,$v,9,1);//(替换第10位的数字为$p)

<<<  运算符,将由自定义分界符间的内容视为字符串,可对其间的变量做处理

echo <<<tem
<script>alert("输入的留言内容太长!");history.go(-1);</script>
tem;

tem是任意字符,<<<后的tem标记着开始。必须以 tem; 结尾,另起一行顶格(前面无空格)书写,否则会报错。

echo <<<'tem'
$a str
tem;

添加单引号 就不会解析里面的变量

base_convert() 函数在任意进制之间转换数字。

is_numeric()函数检测变量是否为数字或数字字符串

urlencode()返回字符串,此字符串中除了 -_. 之外的所有非字母数字字符都将被替换成百分号(%)后跟两位十六进制数.

rawurlencode()功能和urlencode基本一样,采用的是RFC1738编码,因此空格会编码为%20

sprintf() 函数把格式化的字符串写入一个变量中。

in_array()函数在数组中搜索给定的值。

array_search()函数在数组中查找一个键值。如果找到了该值,匹配元素的键名会被返回。

array_key_exists() 函数判断某个数组中是否存在指定的键名

md5(arg[,arg])

函数计算字符串的 MD5 散列,第二的参数规定十六进制或二进制输出格式。

PHP_EOL表示换行符

str_split() 函数把字符串逐字分割到数组中。

split()用正则表达式将字符串分割到数组中

preg_split()通过一个正则表达式分隔字符串

http_build_query()生成 URL-encode 之后的请求字符串

quotemeta() 函数在字符串中某些预定义的字符前添加反斜杠。

mt_rand(A,B) 函数返回A到B的随机整数

compact

创建一个包含变量名和它们的值的数组:

$firstname = "Bill";
$lastname = "Gates";
$age = "60";

$result = compact("firstname", "lastname", "age");

print_r($result);

extract() 正好相反
陆续添加整理。。。

php获取xml数据

作者:matrix 发布时间:2013年10月27日 分类:零零星星

调用某些api后返回的数据可能会是xml格式,这就需要提取相关数据。

如果了解正则匹配的话可以用preg_match()来提取,最好还是用php内置的专用函数来处理xml

 

代码:

$xml = new DOMDocument(); // 首先要建一个DOMDocument对象    
$xml->load('http://api.189.cn/EMP/shorturl/long2short?access_token=76327c4e405b725021640fd629bfc3511382853781284&app_id=120032470000032374&longurl=hhtjim.COM'); // 加载Xml文件     
$postDom = $xml->getElementsByTagName("shorturl")->item(0)->nodeValue;    
echo $postDom;  

 

说明:

第3行"shorturl"为读取的标签名,运行结果将显示http://189.io/ReRTnn

第2行是读取xml文件:


This XML file does not appear to have any style information associated with it. The document tree is shown below. <result> <res_code>0</res_code> <res_message>短地址生成成功。</res_message> <shorturl>http://189.io/ReRTnn</shorturl> </result>

参考:http://developer.51cto.com/art/200912/166247.htm


貌似这个代码也行

$xml = new DOMDocument();  
$xml->load('http://api.189.cn/EMP/shorturl/long2short?access_token=76327c4e405b725021640fd629bfc3511382853781284&app_id=120032470000032374&longurl=hhtjim.COM'); // 加载Xml文件     
foreach($xml->getElementsByTagName('shorturl') as $shorturl);
$value = $shorturl->firstChild->nodeValue;   
echo $value;  

新浪乐库php解析源码

作者:matrix 发布时间:2013年10月26日 分类:零零星星

demo:http://link.hhtjim.com

外链测试:

http://music.sina.com.cn/yueku/i/2850351.html 阅读剩余部分 »

微云网盘外链php源码

作者:matrix 发布时间:2013年10月26日 分类:兼容并蓄 零零星星

微云

微云网盘的10TB空间也要全部利用起来。

申明:此页面的源码已失效,走这里查看最新>>微云网盘php解析源码_更新

外链转换工具:http://link.hhtjim.com/

代码来自破博客

<?php  
//提取微云分享地址  
preg_match('|\/.+\/(\w+)|', $_SERVER['REQUEST_URI'], $res);  
$key = $res ? $res[1] : exit("weiyun URL error!");  
$url = $referer = "http://share.weiyun.com/$key";  
$useragent = "BlackBerry/3.6.0";  

//匹配出下载地址  
$curl = curl_init($url);  
curl_setopt($curl, CURLOPT_RETURNTRANSFER, 1);  
curl_setopt($curl, CURLOPT_USERAGENT, $useragent);  
curl_setopt($curl, CURLOPT_TIMEOUT, 10);  
$src = curl_exec($curl);  
curl_close($curl);  
preg_match('|http://.+sharekey[^"]+|', $src, $res);  
$url = $res ? $res[0] : exit("weiyun URL error! Not find weiyun code!"); 

//伪造referer,获取返回的响应头信息 
$curl = curl_init($url); 
curl_setopt($curl, CURLOPT_HEADER, 1); 
curl_setopt($curl, CURLOPT_RETURNTRANSFER, 1); 
curl_setopt($curl, CURLOPT_REFERER, $referer);  
curl_setopt($curl, CURLOPT_USERAGENT, $useragent); 
curl_setopt($curl, CURLOPT_TIMEOUT, 10); 
$src = curl_exec($curl); 
curl_close($curl); 

//从响应信息头匹配出真实的文件地址并下载 
preg_match('|Location: (.+)\r|', $src, $res); 
$songurl = $res ? $res[1] : exit("Can not get WeiYun Download url!"); 
header("Location: $songurl");  
//echo $songurl;  
?>  

这种方法该不错滴。

使用示例:http://XXX.php/微云URL

http://share.weiyun.com/b500a423288e8d0095d49657fe21438b 阅读剩余部分 »

360云盘外链解析php源码

作者:matrix 发布时间:2013年10月19日 分类:兼容并蓄 零零星星

用火狐的firebug插件看到云盘的下载过程是通过post提交获得json数据(期间会检查REFERER),之后再跳转到下载地址。

查看post提交数据

图片外链演示:

360yunpan图片外链演示

mp3外链失败,但是下载的话没问题(后来才知道是360防盗链的Referer缘故):

源码下载[已失效]:

城通  http://www.400gb.com/file/55758296
baidu http://pan.baidu.com/s/1kTGjyZX
代码[已失效]:

<?php
/** 
 * 2014-1-27更新  
 修改正则匹配
 * Author:不懂就乱来
 * Website:https://www.hhtjim.com
 **/
    error_reporting(0);
    function curl_get($url,$post=0,$carry_header=true,$REFERER_=0,$useragent=0,$add_arry_header=0){
        $ch=curl_init($url);
        //curl_setopt($ch, CURLOPT_HEADER, 1);
        if($carry_header){
            curl_setopt($ch, CURLOPT_HTTPHEADER, array('User-Agent:Mozilla/5.0 (iPhone; U; CPU iPhone OS 3_1_2 like Mac OS X; en-us) AppleWebKit/528.18 (KHTML, like Gecko) Version/4.0 Mobile/7D11 Safari/528.16'));
        }
        if($add_arry_header){
            curl_setopt($ch, CURLOPT_HTTPHEADER, $add_arry_header);
        }
        if ($post) {
            curl_setopt($ch, CURLOPT_POSTFIELDS, $post);
        }
        if($REFERER_){
            curl_setopt($ch, CURLOPT_REFERER,$REFERER_);
        }
        if($useragent){
            curl_setopt($ch, CURLOPT_USERAGENT, $useragent);
        }
        curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
        $get_url = curl_exec($ch);
        curl_close($ch);
        return $get_url;
    }
    function curl_get_http($url,$REFERER_,$add_arry_header=0){
        $curl = curl_init($url);
        curl_setopt($curl, CURLOPT_HEADER, 1);
        curl_setopt($curl, CURLOPT_RETURNTRANSFER, 1);
        if($REFERER_){curl_setopt($curl, CURLOPT_REFERER,$REFERER_);}
        if($add_arry_header){
            curl_setopt($curl, CURLOPT_HTTPHEADER, $add_arry_header);
        }
        curl_setopt($curl, CURLOPT_TIMEOUT, 10);
        $src = curl_exec($curl);
        curl_close($curl);
        return $src ;
    }
if(isset($_GET['360'])){//360云盘下载
        $_url=$_GET['360'];
        $yunpanhtml=curl_get($_url);
        if(emptyempty($yunpanhtml)){
            $_yunpanur__=curl_get_http($_url);
            preg_match("|http:\/\/\w+\.\w+\.yunpan\.cn\/lk\/\w+|",$_yunpanur__,$yunpanhtml_n);
            $yunpanhtml_new=curl_get($yunpanhtml_n[0]);
            }else{
            $yunpanhtml_new=$yunpanhtml;
            }
        $yunpanzz="|SYS_CONF = {[^}]+surl: '(\w+)'[^}]+nid : '(\d+)'[^}]+}|";
        preg_match($yunpanzz,$yunpanhtml_new,$yunpancs);
        $nid = $yunpancs[2] ? $yunpancs[2] : exit('ERROR code: Not found SYS_CONF {nid}');
        $shorturl = $yunpancs[1] ? $yunpancs[1] : exit('ERROR code: Not found SYS_CONF {shorturl}');
        if(!strpos($_url,"yunpan.cn/lk")){//获取postURL地址
            $postht_get=curl_get_http($_url);
            preg_match('|(http:\/\/\w+\.\w+\.yunpan\.cn)\/lk\/\w+|',$postht_get,$posthttpu);
            $post_url=$posthttpu[1].'/share/downloadfile/';
        }
        else{
            preg_match('|(http:\/\/\w+\.\w+\.yunpan\.cn)\/lk\/\w+|',$_url,$yun_url);
            $post_url=$yun_url[1].'/share/downloadfile/';
        }
        $post_data = array('nid' => $nid, 'shorturl' => $shorturl);
        $useragent = "BlackBerry/3.6.0";
        $rt=curl_get($post_url,$post_data,0,$_url,$useragent,0);
        $obj = json_decode($rt);
        $errmsg=$obj->errmsg;
        if(strpos($errmsg,"成功")){
            $dll=$obj->data->downloadurl;
            $dl_out = $dll ? $dll : exit('Can not get YunPan Download url!Please Check <a href="https://www.hhtjim.com/message-wall">here</a> .') ;
            header("Location: $dl_out");}
        else{
            header("Content-Type: text/html; charset=utf-8");
            echo '360云盘:'.$errmsg.'</br>Please Check <a href="https://www.hhtjim.com/message-wall">here</a> .';
        }
    }
    exit('DIE!');
?>

说明:将以上代码保存为yunpan.php

若放在360目录需要修改22行/yunpan.php为/360/yunpan.php

代码不够精简,只有这样了。

1-27更新正则匹配,可保存为任意php文件名。

调用下载:http://XXX/yunpan.php?360=360云盘地址

支持两种360云盘URL格式

http://yunpan.cn/QXJ786DeBjeiW

http://ajvvqzy2v8.l29.yunpan.cn/lk/QXJ786DeBjeiW

转换地址:http://link.hhtjim.com/

 

修改Mini天气预报app

作者:matrix 发布时间:2013年10月3日 分类:兼容并蓄 零零星星

Mini天气预报app v1.2

地址:http://demo.hhtjim.com

修改Mini天气预报app,之前使用的是专业气象的数据,这次改的189 API:http://api.189.cn/huafeng/api/getforecast24?access_token=91d1d9d25c62fd393e113116b07b6f601379679414323&app_id=120032470000032374&city_id=城市ID

参考:http://open.189.cn/index.php?m=api&c=index&a=show&id=359

城市ID列表 :http://open.189.cn/sharedata/cms/uploadfile/2012/1102/113459s3unXsSCAn.zip 阅读剩余部分 »

添加SongTaste音乐外链app

作者:matrix 发布时间:2013年9月24日 分类:兼容并蓄

SongTaste已经不复存在~

转换地址: http://link.hhtjim.com
songtaste.com的音乐打开速度是比较快的,比起百度网盘就songtaste和xiaomi的速度很理想,外链起来比较不错。

图片4831-添加SongTaste音乐外链app

网上找了很多SongTaste外链的方法,大多数都是pc端程序,没看到什么php源码之类的东西,很是蛋疼。 阅读剩余部分 »

添加鼠标滑过图片闪烁的js特效-jquery-opacity-rollover

作者:matrix 发布时间:2013年9月20日 分类:Wordpress 兼容并蓄

图片4842-添加鼠标滑过图片闪烁的js特效-jquery-opacity-rollover

onamae.com的25号免费域名没抢到,倒是看到onamae上的一个图片闪光特效好奇,这就扒了。

看头部的meta写的jquery-opacity-rollover.js文件,不知道这是个啥子插件上的东东。

作者:http://h2ham.seesaa.net

实际效果,文字说明:

假设图片被指定加载此js特效阅读剩余部分 »