自動把純文本轉(zhuǎn)換成Web頁面的php代碼
更新時間:2009年08月27日 13:17:54 作者:
用PHP來快速將純ASCII文本完美地轉(zhuǎn)換成為可讀的HTML標記。
首先讓我們來看一個我朋友希望轉(zhuǎn)換的純文本文件的例子:
以下為引用的內(nèi)容:
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/
相當標準的文本:它有一個標題、一個署名和很多段的文字。把這篇文檔轉(zhuǎn)換成為HTML真正需要做的是使用HTML的分行和分段標記把原文的布局保留在Web頁面上。特殊的標點符號需要被轉(zhuǎn)換成為對應(yīng)的HTML符號,超鏈接需要變得可以點擊。
下面的PHP代碼(列表A)就會完成上面所有的任務(wù):
列表A
讓我們來看看它是如何工作的:
<?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ù)組中的元素。
然后,標題和作者行(我假設(shè)這兩個都是文件的前兩行)都通過array_shift()函數(shù)從數(shù)組里提取出來,放到單獨的變量里。數(shù)組剩下的成員然后被連接成一個字符串。這個字符串現(xiàn)在就包括了整篇文章的正文。
文章正文里像“'”、“<”和“>”這樣的特殊符號通過htmlspecialchars()函數(shù)被轉(zhuǎn)換成相應(yīng)的HTML符號。為了保留文章的原始格式,分行和分段通過nl2br()函數(shù)被轉(zhuǎn)換成HTML的
元素。文章中間多個空格通過簡單的字符串替換被壓縮成為一個空格。
文章正文里的URL用正則表達式來檢測,兩邊是元素。當頁面在Web瀏覽器里顯示的時候,它會把URL轉(zhuǎn)換成為可點擊的超鏈接。
然后用標準的HTML規(guī)則創(chuàng)建輸出的HTML頁面。文章的標題、作者和正文都用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)限。
正如你看到的,假如你有標準格式的ASCII純文本數(shù)據(jù)文件,你可以相當迅速用PHP把它轉(zhuǎn)換成為可使用的Web頁面。如果你已經(jīng)有了一個Web網(wǎng)站,并計劃把新的Web頁面加入進來,那么調(diào)試頁面生成器所使用的模板,使之適應(yīng)原有Web網(wǎng)站的外觀是相當容易的
以下為引用的內(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/
相當標準的文本:它有一個標題、一個署名和很多段的文字。把這篇文檔轉(zhuǎn)換成為HTML真正需要做的是使用HTML的分行和分段標記把原文的布局保留在Web頁面上。特殊的標點符號需要被轉(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ù)組中的元素。
然后,標題和作者行(我假設(shè)這兩個都是文件的前兩行)都通過array_shift()函數(shù)從數(shù)組里提取出來,放到單獨的變量里。數(shù)組剩下的成員然后被連接成一個字符串。這個字符串現(xiàn)在就包括了整篇文章的正文。
文章正文里像“'”、“<”和“>”這樣的特殊符號通過htmlspecialchars()函數(shù)被轉(zhuǎn)換成相應(yīng)的HTML符號。為了保留文章的原始格式,分行和分段通過nl2br()函數(shù)被轉(zhuǎn)換成HTML的
元素。文章中間多個空格通過簡單的字符串替換被壓縮成為一個空格。
文章正文里的URL用正則表達式來檢測,兩邊是元素。當頁面在Web瀏覽器里顯示的時候,它會把URL轉(zhuǎn)換成為可點擊的超鏈接。
然后用標準的HTML規(guī)則創(chuàng)建輸出的HTML頁面。文章的標題、作者和正文都用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)限。
正如你看到的,假如你有標準格式的ASCII純文本數(shù)據(jù)文件,你可以相當迅速用PHP把它轉(zhuǎn)換成為可使用的Web頁面。如果你已經(jīng)有了一個Web網(wǎng)站,并計劃把新的Web頁面加入進來,那么調(diào)試頁面生成器所使用的模板,使之適應(yīng)原有Web網(wǎng)站的外觀是相當容易的
您可能感興趣的文章:
- PHP將HTML轉(zhuǎn)換成文本的實現(xiàn)代碼
- php中將html中的br換行符轉(zhuǎn)換為文本輸入中的換行符
- 使用PHP+JavaScript將HTML頁面轉(zhuǎn)換為圖片的實例分享
- 分享php代碼將360瀏覽器導(dǎo)出的favdb的sqlite數(shù)據(jù)庫文件轉(zhuǎn)換為html
- php實現(xiàn)二進制和文本相互轉(zhuǎn)換的方法
- php將文本文件轉(zhuǎn)換csv輸出的方法
- php實現(xiàn)使用正則將文本中的網(wǎng)址轉(zhuǎn)換成鏈接標簽
- PHP實現(xiàn)把文本中的URL轉(zhuǎn)換為鏈接的auolink()函數(shù)分享
- 把文本中的URL地址轉(zhuǎn)換為可點擊鏈接的JavaScript、PHP自定義函數(shù)
- php導(dǎo)出csv格式數(shù)據(jù)并將數(shù)字轉(zhuǎn)換成文本的思路以及代碼分享
- php實現(xiàn)轉(zhuǎn)換html格式為文本格式的方法
相關(guān)文章
php+mysqli預(yù)處理技術(shù)實現(xiàn)添加、修改及刪除多條數(shù)據(jù)的方法
這篇文章主要介紹了php+mysqli預(yù)處理技術(shù)實現(xiàn)添加、修改及刪除多條數(shù)據(jù)的方法,實例分析了mysqli實現(xiàn)預(yù)處理的原理及使用技巧,可有效提高程序運行效率,非常具有實用價值,需要的朋友可以參考下2015-01-01PHP實現(xiàn)連接設(shè)備、通訊和發(fā)送命令的方法
這篇文章主要介紹了PHP實現(xiàn)連接設(shè)備、通訊和發(fā)送命令的方法,涉及php基于socket實現(xiàn)設(shè)備連接及數(shù)據(jù)通信的相關(guān)技巧,具有一定參考借鑒價值,需要的朋友可以參考下2015-10-10php中session過期時間設(shè)置及session回收機制介紹
在網(wǎng)上可以找到修改配置文件中的session.gc_maxlifetime,如果想了解更多session回收機制,繼續(xù)閱讀2014-05-05PHP結(jié)合Ffmpeg快速搭建流媒體服務(wù)的實踐記錄
這篇文章主要給大家介紹了關(guān)于使用PHP結(jié)合Ffmpeg快速搭建流媒體服務(wù)的相關(guān)資料,文中通過示例代碼介紹的非常詳細,對大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價值,需要的朋友們下面隨著小編來一起學(xué)習(xí)學(xué)習(xí)吧2018-10-10