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( (() === = ((),('.','..' ( () . '/' . ( (() === (语法: = "images/"); ?>2. 搜索和高亮字符串中的关键字highlighter_text(, = ( " ",( = "#4285F4" = ("|()|Ui",".";">$1",语法: = "I like chocolates and I like apples" = "apple" highlighter_text(,?>3. 写入文件= 'blog.csv' = (,'w' = " Hello " .= " World! " .= "rn"(,(?>4. 根据 URL 下载图片imagefromURL(, = curl_init(,CURLOPT_HEADER,0,CURLOPT_RETURNTRANSFER,1,CURLOPT_BINARYTRANSFER,1=curl_exec ( = ("",'w'(,(语法: = "http://koonk.com/images/logo.png" = "koonk.png",?>5. 检测 URL 是否有效isvalidURL( = 0 (filter_var(,FILTER_VALIDATE_URL) !== = 1语法: = "http://koonk.com" = checkvalidURL( ; ?>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(语法: ("Content-type: image/png" qr_code("http://koonk.com","URL"?>7. 计算两个地图坐标之间的距离getDistanceBetweenPointsNew(,,, = - = ((()) * (())) + ((()) * (()) * (( = ( = ( = * 60 * 1.1515 = * 5280 = / 3 = * 1.609344 = * 1000 ('miles','feet','yards','kilometers','meters'语法: = ('lat' => 40.770623,'long' => -73.964367 = ('lat' => 40.758224,'long' => -73.917404 = getDistanceBetweenPointsNew(['lat'],['long'],['lat'],['long' ( => .': '.(,4).''?> 8. 获取一个特定话题标签的所有 TweetsgetTweets( = '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 ordinal(); ?>10. 限制文件下载的速度= 'test-file.zip' = 'your-download-name.zip'<span style="color: #008000">//<span style="color: #008000"> set the download rate limit (=> 20,5 kb/s)<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 style="color: #0000ff">else<span style="color: #000000"> { 11. 把文本转换成图片("Content-type: image/png" = ['text' = imagecreatefrompng("images/button.png" = imagecolorallocate(,255,255 = (imagesx() - 7.5 * ()) / 2 = 9 = 1,fontSize,,,,?>12. 获取远程文件的大小remote_filesize(, = "", = "" = curl_init(,1,CURLOPT_NOBODY,1(!() && !( = ('Authorization: Basic ' . (":",CURLOPT_HTTPHEADER, = curl_exec( = = '/Content-Length:s([0-9].+?)s/' = (,, ([1]) ? [1] : "unknown"语法: = "http://koonk.com/images/logo.png" = remote_filesize( ?>13. 使用 imagebrick 进行 pdf 到图像的转换= './pdf/demo.pdf' = './jpg/demo.jpg'; ('convert "'..'" -colorspace RGB -resize 800 "'..'"',,( == 0) { "Conversion OK" "Conversion failed.".?> 14. 使用 tinyurl 生成短网址get_tiny_url( = = 5,'http://tinyurl.com/api-create.php?url='.,1,CURLOPT_CONNECTTIMEOUT, = curl_exec(语法: = "http://blog.koonk.com/2015/07/Hello-World" = get_tiny_url( ?>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 > save as):ntt" "">FLVntt" "">HQ FLV (if available)ntt" "">MP4ntt" "">3GPn" 16. Facebook 样式的时间戳Facebook (x mins age,y hours ago etc) nicetime((( "No date provided"</span><span style="color: #800080">$periods</span> = <span style="color: #0000ff">array</span>("second","minute","hour","day","week","month","year","decade"<span style="color: #000000">);
</span><span style="color: #800080">$lengths</span> = <span style="color: #0000ff">array</span>("60","60","24","7","4.35","12","10"<span style="color: #000000">);
</span><span style="color: #800080">$now</span> = <span style="color: #008080">time</span><span style="color: #000000">();
</span><span style="color: #800080">$unix_date</span> = <span style="color: #008080">strtotime</span>(<span style="color: #800080">$date</span><span style="color: #000000">);
</span><span style="color: #008000">//</span><span style="color: #008000"> check validity of date</span>
<span style="color: #0000ff">if</span>(<span style="color: #0000ff">empty</span>(<span style="color: #800080">$unix_date</span><span style="color: #000000">)) {
</span><span style="color: #0000ff">return</span> "Bad date"<span style="color: #000000">;
}
</span><span style="color: #008000">//</span><span style="color: #008000"> is it future date or past date</span>
<span style="color: #0000ff">if</span>(<span style="color: #800080">$now</span> > <span style="color: #800080">$unix_date</span><span style="color: #000000">) {
</span><span style="color: #800080">$difference</span> = <span style="color: #800080">$now</span> - <span style="color: #800080">$unix_date</span><span style="color: #000000">;
</span><span style="color: #800080">$tense</span> = "ago"<span style="color: #000000">;
} </span><span style="color: #0000ff">else</span><span style="color: #000000"> {
</span><span style="color: #800080">$difference</span> = <span style="color: #800080">$unix_date</span> - <span style="color: #800080">$now</span><span style="color: #000000">;
</span><span style="color: #800080">$tense</span> = "from now"<span style="color: #000000">;
}
</span><span style="color: #0000ff">for</span>(<span style="color: #800080">$j</span> = 0; <span style="color: #800080">$difference</span> >= <span style="color: #800080">$lengths</span>[<span style="color: #800080">$j</span>] && <span style="color: #800080">$j</span> < <span style="color: #008080">count</span>(<span style="color: #800080">$lengths</span>)-1; <span style="color: #800080">$j</span>++<span style="color: #000000">) {
</span><span style="color: #800080">$difference</span> /= <span style="color: #800080">$lengths</span>[<span style="color: #800080">$j</span><span style="color: #000000">];
}
</span><span style="color: #800080">$difference</span> = <span style="color: #008080">round</span>(<span style="color: #800080">$difference</span><span style="color: #000000">);
</span><span style="color: #0000ff">if</span>(<span style="color: #800080">$difference</span> != 1<span style="color: #000000">) {
</span><span style="color: #800080">$periods</span>[<span style="color: #800080">$j</span>].= "s"<span style="color: #000000">;
}
</span><span style="color: #0000ff">return</span> "<span style="color: #800080">$difference</span> <span style="color: #800080">$periods</span>[<span style="color: #800080">$j</span>] {<span style="color: #800080">$tense</span>}"<span style="color: #000000">;
} 语法: = "2015-07-05 03:45" = nicetime(); ?>(编辑:安卓应用网) 【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容! |
