當(dāng)前位置:首頁(yè) > 新聞中心 > 解決方案
網(wǎng)站建設(shè)中非常有用的四段PHP代碼責(zé)任編輯 :李飛    文章來(lái)源 :星翼創(chuàng)想(16qt59sf.cn)    發(fā)布時(shí)間 :2016-04-08    閱讀次數(shù):4052
在開(kāi)發(fā)網(wǎng)站、app或博客時(shí),代碼片段可以真正地為你節(jié)省時(shí)間。今天,我們就來(lái)分享一下我收集的一些超級(jí)有用的PHP代碼片段。一起來(lái)看一看吧!

1.創(chuàng)建數(shù)據(jù)URI
數(shù)據(jù)URI在嵌入圖像到HTML / CSS / JS中以節(jié)省HTTP請(qǐng)求時(shí)非常有用,并且可以減少網(wǎng)站的加載時(shí)間。下面的函數(shù)可以創(chuàng)建基于$file的數(shù)據(jù)URI。
function data_uri($file, $mime) { $contents=file_get_contents($file); $base64=base64_encode($contents); echo "data:$mime;base64,$base64"; }
2.合并JavaScript和CSS文件
另一個(gè)可以盡量減少HTTP請(qǐng)求和節(jié)省頁(yè)面加載時(shí)間的好建議是:合并你的CSS和JS文件。雖然我更建議大家使用專(zhuān)用插件(例如minify),但使用PHP來(lái)合并文件也非常容易。我們來(lái)看一下:
function combine_my_files($array_files, $destination_dir, $dest_file_name){ if(!is_file($destination_dir . $dest_file_name)){ //continue only if file doesn't exist $content = ""; foreach ($array_files as $file){ //loop through array list $content .= file_get_contents($file); //read each file } //You can use some sort of minifier here //minify_my_js($content); $new_file = fopen($destination_dir . $dest_file_name, "w" ); //open file for writing fwrite($new_file , $content); //write to destination fclose($new_file); return '
'; //output combined file }else{ //use stored file return '
'; //output combine file } }
并且,用法是這樣的:
$files = array( 'http://example/files/sample_js_file_1.js', 'http://example/files/sample_js_file_2.js', 'http://example/files/beautyquote_functions.js', 'http://example/files/crop.js', 'http://example/files/jquery.autosize.min.js', ); echo combine_my_files($files, 'minified_files/', md5("my_mini_file").".js");
3.查看你的電子郵件是否已讀
當(dāng)發(fā)送電子郵件時(shí),你會(huì)希望知道你的郵件是否已讀。這里有一個(gè)非常有趣的代碼片段,它可以記錄閱讀你郵件的IP地址,以及實(shí)際的日期和時(shí)間。
 error_reporting(0); Header("Content-Type: image/jpeg"); //Get IP if (!empty($_SERVER['HTTP_CLIENT_IP'])) { $ip=$_SERVER['HTTP_CLIENT_IP']; } elseif (!empty($_SERVER['HTTP_X_FORWARDED_FOR'])) { $ip=$_SERVER['HTTP_X_FORWARDED_FOR']; } else { $ip=$_SERVER['REMOTE_ADDR']; } //Time $actual_time = time(); $actual_day = date('Y.m.d', $actual_time); $actual_day_chart = date('d/m/y', $actual_time); $actual_hour = date('H:i:s', $actual_time); //GET Browser $browser = $_SERVER['HTTP_USER_AGENT']; //LOG $myFile = "log.txt"; $fh = fopen($myFile, 'a+'); $stringData = $actual_day . ' ' . $actual_hour . ' ' . $ip . ' ' . $browser . ' ' . "\r\n"; fwrite($fh, $stringData); fclose($fh); //Generate Image (Es. dimesion is 1x1) $newimage = ImageCreate(1,1); $grigio = ImageColorAllocate($newimage,255,255,255); ImageJPEG($newimage); ImageDestroy($newimage); ?>
4.從網(wǎng)頁(yè)提取關(guān)鍵詞
正如這小標(biāo)題所說(shuō)的那樣:這個(gè)代碼片段能讓你輕易地從網(wǎng)頁(yè)中提取元關(guān)鍵詞。
$meta = get_meta_tags('http://www.emoticode.net/'); $keywords = $meta['keywords']; // Split keywords $keywords = explode(',', $keywords ); // Trim them $keywords = array_map( 'trim', $keywords ); // Remove empty values $keywords = array_filter

文章轉(zhuǎn)載請(qǐng)保留網(wǎng)址:http://16qt59sf.cn/news/solutions/1647.html

掃碼添加微信
159 8667 8737
24小時(shí)電話(huà)

返回頂部