php實(shí)用代碼片段整理
本文整理歸納了php實(shí)用代碼片段。分享給大家供大家參考,具體如下:
一 從網(wǎng)頁(yè)中提取關(guān)鍵詞
$meta = get_meta_tags('http://www.dbjr.com.cn/');
$keywords = $meta['keywords'];
// Split keywords
$keywords = explode(',', $keywords );
// Trim them
$keywords = array_map( 'trim', $keywords );
// Remove empty values
$keywords = array_filter( $keywords );
print_r( $keywords );
二 查找頁(yè)面上的所有鏈接
使用DOM,你可以在任意頁(yè)面上抓取鏈接,示例如下。
$html = file_get_contents('http://www.example.com');
$dom = new DOMDocument();
@$dom->loadHTML($html);
// grab all the on the page
$xpath = new DOMXPath($dom);
$hrefs = $xpath->evaluate("/html/body//a");
for ($i = 0; $i < $hrefs->length; $i++) {
$href = $hrefs->item($i);
$url = $href->getAttribute('href');
echo $url.'<br />';
}
三 創(chuàng)建數(shù)據(jù)URI
數(shù)據(jù)URI可以幫助將圖像嵌入到HTML/CSS/JS中,從而節(jié)省HTTP請(qǐng)求。下面的函數(shù)可以利用$file創(chuàng)建數(shù)據(jù)URI。
function data_uri($file, $mime) {
$contents=file_get_contents($file);
$base64=base64_encode($contents);
echo "data:$mime;base64,$base64";
}
四 下載和保存遠(yuǎn)程圖片到你的服務(wù)器
當(dāng)你在搭建網(wǎng)站時(shí),很可能會(huì)從遠(yuǎn)程服務(wù)器上下載圖片保存到你自己的服務(wù)器上,下面的代碼就可以幫助你實(shí)現(xiàn)這個(gè)功能。
$image = file_get_contents('http://www.php100.com/image.jpg');
file_put_contents('/images/image.jpg', $image); //Where to save the image
五 移除Microsoft Word HTML標(biāo)簽
當(dāng)你使用Microsoft Word時(shí),會(huì)創(chuàng)建很多標(biāo)簽tag,比如font、span、style、class等,這些標(biāo)簽在Word中十分有用,但當(dāng)你從Word中把文本粘貼到網(wǎng)頁(yè)上,就會(huì)出現(xiàn)很多沒(méi)用的標(biāo)簽。下面實(shí)用的函數(shù)可以幫助你清除所有的Word HTML標(biāo)簽。
function cleanHTML($html) {
///
/// Removes all FONT and SPAN tags, and all Class and Style attributes.
/// Designed to get rid of non-standard Microsoft Word HTML tags.
///
// start by completely removing all unwanted tags
$html = ereg_replace("<(/)?(font|span|del|ins)[^>]*>","",$html);
// then run another pass over the html (twice), removing unwanted attributes
$html = ereg_replace("<([^>]*)(class|lang|style|size|face)=("[^"]*"|'[^']*'|[^>]+)([^>]*)>","<\1>",$html);
$html = ereg_replace("<([^>]*)(class|lang|style|size|face)=("[^"]*"|'[^']*'|[^>]+)([^>]*)>","<\1>",$html);
return $html
}
六 檢測(cè)瀏覽器語(yǔ)言
如果你的網(wǎng)站是多種語(yǔ)言的,下面的代碼可以幫助你檢測(cè)瀏覽器語(yǔ)言,它會(huì)返回客戶(hù)端瀏覽器的默認(rèn)語(yǔ)言。
function get_client_language($availableLanguages, $default='en'){
if (isset($_SERVER['HTTP_ACCEPT_LANGUAGE'])) {
$langs=explode(',',$_SERVER['HTTP_ACCEPT_LANGUAGE']);
foreach ($langs as $value){
$choice=substr($value,0,2);
if(in_array($choice, $availableLanguages)){
return $choice;
}
}
}
return $default;
}
七 保存請(qǐng)求信息到本地
八 excel相互轉(zhuǎn)換日期
//如果去獲取某個(gè)excel日期(格式為:2016-03-12),那么獲取到的是數(shù)字,需要經(jīng)過(guò)轉(zhuǎn)換才能恢復(fù)
public function excelTime($date, $time = false) {
if(function_exists('GregorianToJD')){
if (is_numeric( $date )) {
$jd = GregorianToJD( 1, 1, 1970 );
$gregorian = JDToGregorian( $jd + intval ( $date ) - 25569 );
$date = explode( '/', $gregorian );
$date_str = str_pad( $date [2], 4, '0', STR_PAD_LEFT )
."-". str_pad( $date [0], 2, '0', STR_PAD_LEFT )
."-". str_pad( $date [1], 2, '0', STR_PAD_LEFT )
. ($time ? " 00:00:00" : '');
return $date_str;
}
}else{
// $date=$date>25568? $date+1:25569;
/*There was a bug if Converting date before 1-1-1970 (tstamp 0)*/
$ofs=(70 * 365 + 17+2) * 86400;
$date = date("Y-m-d",($date * 86400) - $ofs).($time ? " 00:00:00" : '');
return $date;
}
}
九 json與數(shù)據(jù)相互轉(zhuǎn)換
1 json轉(zhuǎn)換成數(shù)組
$json = '[{"id":"22","name":"33","descn":"44"}]'; //json格式的數(shù)組轉(zhuǎn)換成 php的數(shù)組
$arr = (Array)json_decode($json);
echo $arr[0]->id; //用對(duì)象的方式訪(fǎng)問(wèn)(這種是沒(méi)有轉(zhuǎn)換成數(shù)組,而是轉(zhuǎn)換成對(duì)象的情況
2 數(shù)組轉(zhuǎn)換成json
$json_arr = array('WebName'=>'11','WebSite'=>'11');
$php_json = json_encode($json_arr); //把php數(shù)組格式轉(zhuǎn)換成 json 格式的數(shù)據(jù)
echo $php_json;
更多關(guān)于PHP相關(guān)內(nèi)容感興趣的讀者可查看本站專(zhuān)題:《php面向?qū)ο蟪绦蛟O(shè)計(jì)入門(mén)教程》、《PHP基本語(yǔ)法入門(mén)教程》、《PHP運(yùn)算與運(yùn)算符用法總結(jié)》、《PHP網(wǎng)絡(luò)編程技巧總結(jié)》、《PHP數(shù)組(Array)操作技巧大全》、《php字符串(string)用法總結(jié)》、《php+mysql數(shù)據(jù)庫(kù)操作入門(mén)教程》及《php常見(jiàn)數(shù)據(jù)庫(kù)操作技巧匯總》
希望本文所述對(duì)大家PHP程序設(shè)計(jì)有所幫助。
相關(guān)文章
php 強(qiáng)制下載文件實(shí)現(xiàn)代碼
php 強(qiáng)制下載文件實(shí)現(xiàn)代碼。需要的朋友可以過(guò)來(lái)參考下,希望對(duì)大家有所幫助2013-10-10
PHP實(shí)現(xiàn)的簡(jiǎn)單日歷類(lèi)
這篇文章主要介紹了PHP實(shí)現(xiàn)的簡(jiǎn)單日歷類(lèi),可實(shí)現(xiàn)簡(jiǎn)單的日期顯示功能,并高亮標(biāo)注當(dāng)前日期,具有一定的實(shí)用價(jià)值,需要的朋友可以參考下2014-11-11
PHP使用enqueue/amqp-lib實(shí)現(xiàn)rabbitmq任務(wù)處理
這篇文章主要為大家詳細(xì)介紹了PHP如何使用enqueue/amqp-lib實(shí)現(xiàn)rabbitmq任務(wù)處理,文中的示例代碼講解詳細(xì),感興趣的小伙伴可以學(xué)習(xí)一下2024-03-03
php實(shí)現(xiàn)概率性隨機(jī)抽獎(jiǎng)代碼
本文給大家分享的是使用php根據(jù)獎(jiǎng)品的權(quán)重來(lái)實(shí)現(xiàn)概率性隨機(jī)抽獎(jiǎng)的代碼,非常的使用,有類(lèi)似需求的小伙伴,可以拿去參考下2016-01-01
探討如何把session存入數(shù)據(jù)庫(kù)
本篇文章是對(duì)如何把session存入數(shù)據(jù)庫(kù)的方法進(jìn)行了詳細(xì)的分析介紹,需要的朋友參考下2013-06-06
php 數(shù)組動(dòng)態(tài)添加實(shí)現(xiàn)代碼(最土團(tuán)購(gòu)系統(tǒng)的價(jià)格排序)
最近在實(shí)現(xiàn)最土團(tuán)購(gòu)系統(tǒng)的價(jià)格排序功能,需要對(duì)$oc數(shù)組進(jìn)行擴(kuò)展,經(jīng)過(guò)測(cè)試用下面的方法即可。2011-12-12
php刪除txt文件指定行及按行讀取txt文檔數(shù)據(jù)的方法
這篇文章主要介紹了php刪除txt文件指定行及按行讀取txt文檔數(shù)據(jù)的方法,涉及php針對(duì)txt文件的按行讀取、刪除等操作技巧,需要的朋友可以參考下2017-01-01

