加入收藏 | 设为首页 | 会员中心 | 我要投稿 安卓应用网 (https://www.0791zz.com/)- 科技、建站、经验、云计算、5G、大数据,站长网!
当前位置: 首页 > 编程开发 > PHP > 正文

PHP实用代码片段(四)

发布时间:2020-05-25 03:05:20 所属栏目:PHP 来源:互联网
导读:1. 删除文件夹内容function Delete($path){if (is_dir($path) === true){$files = array_diff(scandir($path), array(., ..));foreach ($files as $file){Delete(realpath($path) . / . $file);}return rmdir($path);}else if (is_file($path) === true){retu

1. 删除文件夹内容

Delete( (() === = ((),('.','..' ( () . '/' . ( (() === (

语法:

2. 搜索和高亮字符串中的关键字

highlighter_text(, = ( " ",( = "#4285F4" = ("|()|Ui",".";">$1",

语法:

3. 写入文件

4. 根据 URL 下载图片

imagefromURL(, = curl_init(,CURLOPT_HEADER,0,CURLOPT_RETURNTRANSFER,1,CURLOPT_BINARYTRANSFER,1=curl_exec ( = ("",'w'(,(

语法:

5. 检测 URL 是否有效

isvalidURL( = 0 (filter_var(,FILTER_VALIDATE_URL) !== = 1

语法:

6. 生成二维码

qr_code(, = "TXT", ='150',='L',='0' = ("URL" =--> "http://","TEL" => "TEL:","TXT"=>"","EMAIL" => "MAILTO:"(!(,("URL","TEL","TXT","EMAIL" = "TXT" (!('/^'.[].'/', = ("","",[]). = = (,CURLOPT_URL,'http://chart.apis.google.com/chart',CURLOPT_POST,,CURLOPT_POSTFIELDS,'chs='..'x'..'&cht=qr&chld='..'|'..'&chl='.,,CURLOPT_TIMEOUT,30 = curl_exec(

语法:

7. 计算两个地图坐标之间的距离

getDistanceBetweenPointsNew(,,, = - = ((()) * (())) + ((()) * (()) * (( = ( = ( = * 60 * 1.1515 = * 5280 = / 3 = * 1.609344 = * 1000 ('miles','feet','yards','kilometers','meters'

语法:

40.770623,'long' => -73.964367 = ('lat' => 40.758224,'long' => -73.917404 = getDistanceBetweenPointsNew(['lat'],['long'],['lat'],['long' ( => .': '.(,4).'
'?>

8. 获取一个特定话题标签的所有 Tweets

getTweets( = 'http://search.twitter.com/search.atom?q='.( "

Connecting to ...

" = curl_init(, = curl_exec (Response:

"; //echo "
".htmlspecialchars($xml)."
"; = 0 = SimpleXMLElement( (->entry = (-> = (->author-> = (-> = -> "

Tweet from "..": ".." Posted ".('n/j/y g:i a',)."

"

9. 添加 th,st,nd 或者 rd 作为数字的后缀

ordinal( = () % 10 = ((() %100 < 21 && () %100 > 4) ? 'th' : (( < 4) ? ( < 3) ? ( < 2) ? ( < 1? 'th' : 'st' : 'nd' : 'rd' : 'th' .

语法:

10. 限制文件下载的速度

<span style="color: #800080">$download_rate = 20.5<span style="color: #000000">;
<span style="color: #0000ff">if(<span style="color: #008080">file_exists(<span style="color: #800080">$local_file) && <span style="color: #008080">is_file(<span style="color: #800080">$local_file<span style="color: #000000">)) {
<span style="color: #008000">//<span style="color: #008000"> send headers
<span style="color: #008080">header('Cache-control: private'<span style="color: #000000">);
<span style="color: #008080">header('Content-Type: application/octet-stream'<span style="color: #000000">);
<span style="color: #008080">header('Content-Length: '.<span style="color: #008080">filesize(<span style="color: #800080">$local_file<span style="color: #000000">));
<span style="color: #008080">header('Content-Disposition: filename='.<span style="color: #800080">$download_file<span style="color: #000000">);

</span><span style="color: #008000"&gt;//</span><span style="color: #008000"&gt; flush content</span>
<span style="color: #008080"&gt;flush</span><span style="color: #000000"&gt;();    
</span><span style="color: #008000"&gt;//</span><span style="color: #008000"&gt; open file stream</span>
<span style="color: #800080"&gt;$file</span> = <span style="color: #008080"&gt;fopen</span>(<span style="color: #800080"&gt;$local_file</span>,"r"<span style="color: #000000"&gt;);    
</span><span style="color: #0000ff"&gt;while</span>(!<span style="color: #008080"&gt;feof</span>(<span style="color: #800080"&gt;$file</span><span style="color: #000000"&gt;)) {

    </span><span style="color: #008000"&gt;//</span><span style="color: #008000"&gt; send the current file part to the browser</span>
    <span style="color: #0000ff"&gt;print</span> <span style="color: #008080"&gt;fread</span>(<span style="color: #800080"&gt;$file</span>,<span style="color: #008080"&gt;round</span>(<span style="color: #800080"&gt;$download_rate</span> * 1024<span style="color: #000000"&gt;));    

    </span><span style="color: #008000"&gt;//</span><span style="color: #008000"&gt; flush the content to the browser</span>
    <span style="color: #008080"&gt;flush</span><span style="color: #000000"&gt;();

    </span><span style="color: #008000"&gt;//</span><span style="color: #008000"&gt; sleep one second</span>
    <span style="color: #008080"&gt;sleep</span>(1<span style="color: #000000"&gt;);    
}    

</span><span style="color: #008000"&gt;//</span><span style="color: #008000"&gt; close file stream</span>
<span style="color: #008080"&gt;fclose</span>(<span style="color: #800080"&gt;$file</span><span style="color: #000000"&gt;);}

<span style="color: #0000ff">else<span style="color: #000000"> {
<span style="color: #0000ff">die('Error: The file '.<span style="color: #800080">$local_file.' does not exist!'<span style="color: #000000">);
}
?>

11. 把文本转换成图片

12. 获取远程文件的大小

remote_filesize(, = "", = "" = curl_init(,1,CURLOPT_NOBODY,1(!() && !( = ('Authorization: Basic ' . (":",CURLOPT_HTTPHEADER, = curl_exec( = = '/Content-Length:s([0-9].+?)s/' = (,, ([1]) ? [1] : "unknown"

语法:

13. 使用 imagebrick 进行 pdf 到图像的转换

".?>

14. 使用 tinyurl 生成短网址

get_tiny_url( = = 5,'http://tinyurl.com/api-create.php?url='.,1,CURLOPT_CONNECTTIMEOUT, = curl_exec(

语法:

15. youtube 下载链接生成器

str_between(,, = " ".; = (,); ( == 0) ""; += (); = (,,) - ; (,, = ['youtube' = ( = str_between(,"&video_id=","&" = str_between(,"&t=","&" = "http://www.youtube.com/get_video?video_id=&t=" = "http://www.youtube.com/get_video?video_id=&t=&fmt=6" = "http://www.youtube.com/get_video?video_id=&t=&fmt=18" = "http://www.youtube.com/get_video?video_id=&t=&fmt=17" "ttDownload (right-click &gt; save as)&#58;ntt" "">FLVntt" "">HQ FLV (if available)ntt" "">MP4ntt" "">3GP

n"

16. Facebook 样式的时间戳

Facebook (x mins age,y hours ago etc)

nicetime((( "No date provided"</span><span style="color: #800080"&gt;$periods</span> = <span style="color: #0000ff"&gt;array</span>("second","minute","hour","day","week","month","year","decade"<span style="color: #000000"&gt;); </span><span style="color: #800080"&gt;$lengths</span> = <span style="color: #0000ff"&gt;array</span>("60","60","24","7","4.35","12","10"<span style="color: #000000"&gt;); </span><span style="color: #800080"&gt;$now</span> = <span style="color: #008080"&gt;time</span><span style="color: #000000"&gt;(); </span><span style="color: #800080"&gt;$unix_date</span> = <span style="color: #008080"&gt;strtotime</span>(<span style="color: #800080"&gt;$date</span><span style="color: #000000"&gt;); </span><span style="color: #008000"&gt;//</span><span style="color: #008000"&gt; check validity of date</span> <span style="color: #0000ff"&gt;if</span>(<span style="color: #0000ff"&gt;empty</span>(<span style="color: #800080"&gt;$unix_date</span><span style="color: #000000"&gt;)) { </span><span style="color: #0000ff"&gt;return</span> "Bad date"<span style="color: #000000"&gt;; } </span><span style="color: #008000"&gt;//</span><span style="color: #008000"&gt; is it future date or past date</span> <span style="color: #0000ff"&gt;if</span>(<span style="color: #800080"&gt;$now</span> > <span style="color: #800080"&gt;$unix_date</span><span style="color: #000000"&gt;) { </span><span style="color: #800080"&gt;$difference</span> = <span style="color: #800080"&gt;$now</span> - <span style="color: #800080"&gt;$unix_date</span><span style="color: #000000"&gt;; </span><span style="color: #800080"&gt;$tense</span> = "ago"<span style="color: #000000"&gt;; } </span><span style="color: #0000ff"&gt;else</span><span style="color: #000000"&gt; { </span><span style="color: #800080"&gt;$difference</span> = <span style="color: #800080"&gt;$unix_date</span> - <span style="color: #800080"&gt;$now</span><span style="color: #000000"&gt;; </span><span style="color: #800080"&gt;$tense</span> = "from now"<span style="color: #000000"&gt;; } </span><span style="color: #0000ff"&gt;for</span>(<span style="color: #800080"&gt;$j</span> = 0; <span style="color: #800080"&gt;$difference</span> >= <span style="color: #800080"&gt;$lengths</span>[<span style="color: #800080"&gt;$j</span>] &amp;&amp; <span style="color: #800080"&gt;$j</span> < <span style="color: #008080"&gt;count</span>(<span style="color: #800080"&gt;$lengths</span>)-1; <span style="color: #800080"&gt;$j</span>++<span style="color: #000000"&gt;) { </span><span style="color: #800080"&gt;$difference</span> /= <span style="color: #800080"&gt;$lengths</span>[<span style="color: #800080"&gt;$j</span><span style="color: #000000"&gt;]; } </span><span style="color: #800080"&gt;$difference</span> = <span style="color: #008080"&gt;round</span>(<span style="color: #800080"&gt;$difference</span><span style="color: #000000"&gt;); </span><span style="color: #0000ff"&gt;if</span>(<span style="color: #800080"&gt;$difference</span> != 1<span style="color: #000000"&gt;) { </span><span style="color: #800080"&gt;$periods</span>[<span style="color: #800080"&gt;$j</span>].= "s"<span style="color: #000000"&gt;; } </span><span style="color: #0000ff"&gt;return</span> "<span style="color: #800080"&gt;$difference</span> <span style="color: #800080"&gt;$periods</span>[<span style="color: #800080"&gt;$j</span>] {<span style="color: #800080"&gt;$tense</span>}"<span style="color: #000000"&gt;;

}

语法:

(编辑:安卓应用网)

【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容!

    推荐文章
      热点阅读