因為Notepad++對於PHP的關鍵字不足,有些關鍵字都不會變色,所以寫了一個class能去抓官方PHP的網頁函式清單

網友bcse提醒我其實PHP有內建的get_defined_functions、get_class_methods…等函式,可以直接取得相關關鍵字,看來我做了蠢事了。 不過也多虧這次的做法,讓我了解phpQuery的使用方法
//因為若抓起整個函式庫,時間會有點久,所以延長執行時間
set_time_limit(300);
//建立phpkeyword物件
$phpKeyword = new phpKeyword();
//抓取官方所有函式表的關鍵字
foreach($phpKeyword->refList() as $ref){
	//echo '讀取'.$ref.'的books';
	foreach($phpKeyword->bookList($ref) as $book){
		//echo '  讀取'.$book.'的functions';
		foreach($phpKeyword->functionList($book) as $func){
			//echo '    取得'.$func';
			echo $func.' ';
		}
	}
}
未來考慮將此類別寫一個讓大家有選擇性的下載關鍵字的網頁,下列為phpword的類別檔
/************************************************
  * 類別功能:PHP 關鍵字產生器
  * 授權方式:MIT license
  * 程式作者:Liao San-Kai
  * 撰寫日期:2008-10-03
  * 作者網誌:http://liaosankai.pixnet.net/blog
  * 注意事項:此class需載入phpQuery.php函式庫
  *         http://code.google.com/p/phpquery/
  ***********************************************/
class phpKeyword {
	//==========================
	// 建構子
	//==========================
	function phpKeyword(){
		include_once('phpQuery.php');
	}
	//==========================
	//取得官方的ref清單
	//==========================
	function refList(){
		$url = 'http://www.php.net/manual/en/funcref.php';
		$html = file_get_contents($url);
		$list = phpQuery::newDocument($html)->find('.title')
		                                    ->next()
											->next()
											->children(':has(ul)');
		foreach(pq($list) as $li) {
			$refs[] = pq($li)->find('a')->attr('href');
		}
		if(is_array($refs)){
			return $refs;
		} else {
			return array();
		}
	}
	//==========================
	//取得指定ref的book清單
	//==========================
	function bookList($ref){
		$url = 'http://www.php.net/manual/en/'.$ref;
		$html = file_get_contents($url);
		$list = phpQuery::newDocument($html)->find('.title')
		                                    ->next()
											->children(':has(ul)');
		foreach(pq($list) as $li) {
			$books[] = pq($li)->find('a')->attr('href');
		}
		if(is_array($books)){
			return $books;
		} else {
			return array();
		}		
	}
	//============================
	//取得指定book的function清單
	//============================
	function functionList($book){
		$url = 'http://www.php.net/manual/en/'.$book;
		$html = file_get_contents($url);
		$book = explode('.',$book);
		$book[0] = 'ref';
		$list = phpQuery::newDocument($html)->find('.title')
		                                    ->next()
											->find('li > a[href="'.join('.',$book).'"]')
											->parent()
											->find('li a');
		foreach(pq($list) as $li) {
			$functions[] = pq($li)->text();
		}
		if(is_array($functions)){
			return $functions;
		} else {
			return array();
		}		
	}
}
arrow
arrow
    全站熱搜

    低溫烘培 發表在 痞客邦 留言(0) 人氣()