色尼玛亚洲综合影院,亚洲3atv精品一区二区三区,麻豆freexxxx性91精品,欧美在线91

自動把純文本轉(zhuǎn)換成Web頁面的php代碼

首先讓我們來看一個我朋友希望轉(zhuǎn)換的純文本文件的例子:
以下為引用的內(nèi)容:
復(fù)制代碼 代碼如下:
  Green for Mars!
  John R. Doe
  The idea of little green men from Mars, long a staple of science fiction, may soon turn out to be less fantasy and more fact.
  Recent samples sent by the latest Mars exploration team indicate a high presence of chlorophyll in the atmosphere. Chlorophyll, you will recall, is what makes plants green. It's quite likely, therefore, that organisms on Mars will have, through continued exposure to the green stuff, developed a greenish tinge on their outer exoskeleton.
  An interview with Dr. Rushel Bunter, the head of ASDA's Mars Colonization Project blah blah...
  What does this mean for you? Well, it means blah blahblah...
  Track follow-ups to this story online at http://www.mars-connect.dom/. To see pictures of the latest samples, log on to http://www.asdamcp.dom/galleries/220/

相當(dāng)標(biāo)準(zhǔn)的文本:它有一個標(biāo)題、一個署名和很多段的文字。把這篇文檔轉(zhuǎn)換成為HTML真正需要做的是使用HTML的分行和分段標(biāo)記把原文的布局保留在Web頁面上。特殊的標(biāo)點符號需要被轉(zhuǎn)換成為對應(yīng)的HTML符號,超鏈接需要變得可以點擊。
下面的php代碼(列表A)就會完成上面所有的任務(wù):
列表A
讓我們來看看它是如何工作的:
復(fù)制代碼 代碼如下:
<?php
// set source file name and path
$source = "toi200686.txt";
// read raw text as array
$raw = file($source) or die("Cannot read file");
// retrieve first and second lines (title and author)
$slug = array_shift($raw);
$byline = array_shift($raw);
// join remaining data into string
$data = join('', $raw);
// replace special characters with HTML entities
// replace line breaks with <br />
$html = nl2br(htmlspecialchars($data));
// replace multiple spaces with single spaces
$html = preg_replace('/ss+/', ' ', $html);
// replace URLs with <a href...> elements
$html = preg_replace('/s(w+://)(S+)/', ' <a href="" target="_blank"></a>', $html);
// start building output page
// add page header
$output =<<< HEADER
<html>
<head>
<style>
.slug {font-size: 15pt; font-weight: bold}
.byline { font-style: italic }
</style>
</head>
<body>
HEADER;
// add page content
$output .= "<div class='slug'>$slug</div>";
$output .= "<div class='byline'>By $byline</div><p />";
$output .= "<div>$html</div>";
// add page footer
$output .=<<< FOOTER
</body>
</html>
FOOTER;
// display in browser
echo $output;
// AND/OR
// write output to a new .html file
file_put_contents(basename($source, substr($source, strpos($source, '.'))) . ".html", $output) or die("Cannot write file");
?>

第一步是把純ASCII文件讀取到一個php數(shù)組里。這通過file()函數(shù)很容易就可以完成,這個函數(shù)會把文件的每一行都轉(zhuǎn)換成為一個用數(shù)字索引的數(shù)組中的元素。
然后,標(biāo)題和作者行(我假設(shè)這兩個都是文件的前兩行)都通過array_shift()函數(shù)從數(shù)組里提取出來,放到單獨的變量里。數(shù)組剩下的成員然后被連接成一個字符串。這個字符串現(xiàn)在就包括了整篇文章的正文。
文章正文里像“'”、“<”和“>”這樣的特殊符號通過htmlspecialchars()函數(shù)被轉(zhuǎn)換成相應(yīng)的HTML符號。為了保留文章的原始格式,分行和分段通過nl2br()函數(shù)被轉(zhuǎn)換成HTML的
元素。文章中間多個空格通過簡單的字符串替換被壓縮成為一個空格。
文章正文里的URL用正則表達式來檢測,兩邊是元素。當(dāng)頁面在Web瀏覽器里顯示的時候,它會把URL轉(zhuǎn)換成為可點擊的超鏈接。
然后用標(biāo)準(zhǔn)的HTML規(guī)則創(chuàng)建輸出的HTML頁面。文章的標(biāo)題、作者和正文都用CSS樣式規(guī)則格式化。盡管這段腳本沒有這樣做,但是你可以在這個地方自定義最終頁面的外觀,你可以向模板添加圖形元素、顏色或者其他眩目的內(nèi)容。
一旦HTML頁面構(gòu)建完成,它就可以被送到瀏覽器或者用file_put_contents()保存為靜態(tài)文件。要注意的是,在保存的時候,原來的文件名會被分解,一個新的文件名(叫做filename.html)會為新創(chuàng)建的Web頁面創(chuàng)建。你然后就可以把這個Web頁面發(fā)布到Web服務(wù)器上、保存到光盤上或者對它進行進一步編輯。
注意:在使用這個腳本創(chuàng)建和保存HTML文件到磁盤的時候,你要確保這個腳本對文件保存的目錄有寫權(quán)限。
正如你看到的,假如你有標(biāo)準(zhǔn)格式的ASCII純文本數(shù)據(jù)文件,你可以相當(dāng)迅速用php把它轉(zhuǎn)換成為可使用的Web頁面。如果你已經(jīng)有了一個Web網(wǎng)站,并計劃把新的Web頁面加入進來,那么調(diào)試頁面生成器所使用的模板,使之適應(yīng)原有Web網(wǎng)站的外觀是相當(dāng)容易的

php技術(shù)自動把純文本轉(zhuǎn)換成Web頁面的php代碼,轉(zhuǎn)載需保留來源!

鄭重聲明:本文版權(quán)歸原作者所有,轉(zhuǎn)載文章僅為傳播更多信息之目的,如作者信息標(biāo)記有誤,請第一時間聯(lián)系我們修改或刪除,多謝。

主站蜘蛛池模板: 嵊泗县| 昆明市| 宜都市| 峨眉山市| 栾城县| 钟祥市| 汕头市| 新田县| 台中市| 延吉市| 襄樊市| 宜阳县| 乌审旗| 疏勒县| 伊川县| 开封市| 修文县| 石屏县| 高阳县| 沙湾县| 通化市| 金坛市| 鄂州市| 凉城县| 平利县| 无锡市| 扎鲁特旗| 杭锦后旗| 贺州市| 仙游县| 牙克石市| 武川县| 科尔| 修水县| 刚察县| 高安市| 涡阳县| 富民县| 巨野县| 余庆县| 景洪市|