CH4 格式化文字與程式碼



一、撰寫符合標準的網頁


加入DOCTYPE宣告

參考資料:「合法DTD」、「HTML驗証器

二、顯示外國和特殊字體


●在標籤加上
<meta http-equiv="Content-Type" content="text/html; charset=utf-8">
●在.htaccess檔中修改伺服器的預設字型,明確指定哪個網頁用什麼字型
<Files "russian.html">
AddCharset windows-1252 .html
</Files>

使用<meta>標籤衍生的問題:
●網頁伺服器的設定可能會蓋過文件本身的設定:
解決方法1、在網站主目錄下建一個.htaccess檔,在其中加入 AddType 'text/html; charset=utf-8' .html
解決方法2、修改目錄中特定檔案使用的字元集,
  <Files "russian.html">
  AddCharset windows-1252 .html
  </Files>
  <Files "japanese.html">
  AddCharset Shift_JIS .html
  </Files>
  <Files "arabic.html">
  AddCharset iso-8859-6-1252 .html
  </Files>
參考資料:「Unicode組織」、「FileFormat.info

三、為展示和內容文字挑選字型大小


參考資料:「Sane CSS Typography」、「Scalable Inman Flash Replacement

四、在靜態網頁裡引用動態內容


在靜態網頁裡使用伺服器內建的SSI剖析語法<!--#include -->,<!--#echo -->,<!-- #exec -->。
顯示日期:<!--echo var="DATE_LOCAL" -->
顯示最近修改日:<!--echo var="LAST_MODIFIED" -->
加上日期格式:<!--CONFIG TIMEFMT="%d" -->
顯示script的執行結果:<!--#include virtual="/cgi-bin/hello.cgi"-->
傳遞參數給script:<!--#include  virtual="/cgi-bin/catalog.cgi?items=sale&show=5"-->

參考資料:「Apaceh Software Foundation伺服端引人

五、在長英文字中任意加入連字號


使用 &#173 或 &shy;

六、把文字區塊切成多個網頁


這個程式可以將資料分頁顯示。

<?
$text="從資料庫或檔案讀取完整的文章內容";
function processText($texttoProcess){
  $texttoProcess=str_replace("\n\n", "\n</p>\n<p>\n", $texttoProcess);
  return $texttoProcess;
}

$text_limit=275;
$text_array=explode(" ", $text);
$text_total_words=count($text_array);
if(! isset($step)){
  $start=0;
  $step=$text_limit;
}
for($x=$start; $x<$step; $x++){
  $text_display .= $text_array[$x]." ";
}
$text_display=processText($text_display);
if($start > 0){  //第一頁
  $pstart=$start - $text_limit;
  $pstep=$step - $text_limit;
  $text_display="<a href=\"word_break.php?start=".$pstart."&step=".$pstep."\">< Prev page</a> ... ".$text_display;
}
if($text_total_words > $step){  //還有內容可以顯示
  $nstart=$start + $text_limit;
  $nstep=$step + $text_limit;
  $text_display=$text_display."... ;<a href=\"word_break.php?start=".$nstart."&step=".$nstep."\">Next page></a>";
}
echo "<p>\n".$text_display."\n</p>";
?>


七、把資料內容重新格式化成HTML


使用PHP的htmlentities()和str_replace()函式。
function ProcessText($text){
  $text=str_replace("&gt;", ">", $text);
  $text=str_replace("&lt;", "<", $text);
  $text=str_replace("\r\n\r\n", " </p>\n<p> ", $text);
  $text=str_replace("\r\n", " </p>\n<p> ", $text);
  $text=str_replace("\n\n", " </p>\n<p> ", $text);
  $text=str_replace("\n ", " </p>\n<p> ", $text);
  return $text;
}

PHP內建一些函式
addslaches():跳脫單引號、雙引號、反斜線(若PHP有啟動magic_quotes_gpc則不要使用本函式)
stripslashes():反跳脫單引號、雙引號、反斜線(若PHP有啟動magic_quotes_gpc則不要使用本函式)
nl2br():把\n替代成<br>
htmlspecialchars():處理特殊字元轉換成HTML表示法。「&」、「>」、「<」。
htmlentities():將具有對應html表示法的字元都替換掉

寫入資料庫前先格式化
function processInsert($text){
  $text=addslashes($text);
  $text=htmlentities($text);
  $text=str_replace("&gt;", ">", $text);
  $text=str_replace("&lt;", "<", $text);
  return $text;
}

八、最佳化網頁程式碼


最佳化程式碼主要是要來緘少HTML檔的檔案大小,進而加快瀏覽時的下載速度。但這樣不免要犧牲一些檔案的可讀性,所以如何取捨可能因人而異。

使用perl來做字串替代
perl -pi -e 's/>\s+</></g' *.html
perl -pi -e 's/.\s\s/.\s/g' *.html
perl -pi -e 's/>\t+</\t/g' *.html
perl -pi -e 's/>\r+</\r/g' *.html

參考資訊:「正規表示式入門」、「軟體HTML Code Cleaner」、「軟體HTML-Optimizer」、「Port80 Software
arrow
arrow
    全站熱搜

    jck11 發表在 痞客邦 留言(0) 人氣()