本文目錄一覽:
如何用PHP輸出靜態頁面?
一種是利用模板技術,另一種是用ob系列函數。兩種方法,看起來都差不多,但是實際上,卻是不同的。
第一種:利用模板目前PHP的模板可以說是很多了,有功能強大的smarty,還有簡單易用的smart
template等。
它們每一種模板,都有一個獲取輸出內容的函數。
我們生成靜態頁面的方法,就是利用了這個函數。
用這個方法的優點是,代碼比較清晰,可讀性好。
$t
=
new
Smarty;
$t-assign(“title”,”Hello
World!”);
$content
=
$t-fetch(“templates/index.htm”);
//這裡的
fetch()
就是獲取輸出內容的函數,現在$content變量裏面,就是要顯示的內容了
$fp
=
fopen(“archives/2005/05/19/0001.html”,
“w”);
fwrite($fp,
$content);
fclose($fp);?第二種方法:利用ob系列的函數這裡用到的函數主要是
ob_start(),
ob_end_flush(),
ob_get_content(),
其中ob_start()是打開瀏覽器緩衝區的意思,
打開緩衝後,所有來自PHP程序的非文件頭信息均不會發送,
而是保存在內部緩衝區,直到你使用了ob_end_flush().
而這裡最重要的一個函數,就是ob_get_contents(),
這個函數的作用是獲取緩衝區的內容,相當於上面的那個fetch(),
道理一樣的。代碼:
PHP:如何在控制台輸出內容呢?求解
使用echo、print_r 等輸出函數,其步驟如下:
需要準備的材料分別是:電腦、php編輯器、瀏覽器。
1、首先,打開php編輯器,新建php文件,例如:index.php。
2、在index.php中,輸入代碼:echo ‘hello, world!br/’;print_r([1, 2]);。
3、瀏覽器運行index.php頁面,此時發現相關內容被輸出了。
php yar頁面輸出樣式是怎麼實現的
1、安裝msgpack、yar、yaf三個php擴展
2、編譯yar的時候,使用./configure –enable-msgpack –with-php-config=/usr/local/php/bin/pgp-config參數,–enable-msgpack參數是開啟packager對yar的支持
3、php -i|grep msgpack,如果有yar.packager = msgpack = msgpack說明yar已經支持了msgpack
4、測試頁面
class YarCheckKeyword {
protected static $HOSTNAME = ‘kwdt.yarc.service.weibo.com’;
protected static $PORT = ‘7002’;
/**
*
* 請求Kwdt Server
*
* @param string $text 文本字符串
*
* @param array $types 關鍵詞類型
*
* @param int $return_text 是否返回命中的關鍵詞 1.是 0.否 這裡不需要返回
*
* @return array
*
*/
public function connectKwdt_Server($text, $return_text = 1, $types=array(1, 2, 3), $withoutsass = false) {
if (!class_exists(“Yar_client”) || !$text || !$types) {
return “yar_client no exists\n”;
}
$funcname = “detect”;
$host = YarCheckKeyword::$HOSTNAME;
$port = YarCheckKeyword::$PORT;
try{
$client = new Yar_Client(“tcp://$host:$port”);
$response = $client-$funcname($text, $return_text, $types);
return $response;
}catch (Exception $e){
print_r($e);
}
}
}
$text=’aaaaaaaaaaa’;
$obj=new YarCheckKeyword();
$a=$obj-connectKwdt_Server($text);
print_r($a);
5、上述代碼保存成文件,使用php執行,測試結果為下面內容說明成功
Array
(
[0] = -1
[1] = no keyword occured
)
如何用PHP實現頁面的GZIP壓縮輸出
第一步,你需要對php的設置如下:
php.ini: output_buffering = Off output_handler = ob_gzhandler zlib.output_compression = Off zlib.output_compression_level = -1
第二步,你需要在apache下增加如下設置:
AddOutputFilter DEFLATE html php js css
這樣就可以對html php js css進行gzip壓縮了。
第三步,你需要使用如下php壓縮html並輸出到客戶端的函數:
function compress_html($string) { return ltrim(rtrim(preg_replace(array(“/ *([^ ]*) */”,”//”,”‘/\*[^*]*\*/'”,”/\r\n/”,”/\n/”,”/\t/”,’/[ ]+/’), array(“\\1″,”,”,”,”,”,”),$string))); }
上面的這個正則表達式,很強大的哦,經過我本人親自測試可使用。
通過以上方法,你就可以將你的html代碼壓縮然後輸出給客戶端了。不信你可以查看源代碼,就是一行,網頁瞬間壓縮很小。
原創文章,作者:XMZC,如若轉載,請註明出處:https://www.506064.com/zh-hk/n/140418.html