2012-02-10 135 views
0

我有以下代碼,它可以很好地從SQL表中格式化文本。雖然看起來有點長。PHP mysql格式化文本

它將從換行符創建段落,但忽略標題和列表標籤(不換那些在「P」標籤。

任何人都可以看到一個明顯的方式來凝聚呢?

<?php 

function format_html($content) 
{ 
    $content = str_replace("<h1>\r\n", "<h1>", $content); 
    $content = str_replace("</h1>\r\n", "</h1><p>", $content); 
    $content = str_replace("<h2>\r\n", "<h2>", $content); 
    $content = str_replace("</h2>\r\n", "</h2><p>", $content); 
    $content = str_replace("<h3>\r\n", "<h3>", $content); 
    $content = str_replace("</h3>\r\n", "</h3><p>", $content); 
    $content = str_replace("<h4>\r\n", "<h4>", $content); 
    $content = str_replace("</h4>\r\n", "</h4><p>", $content); 
    $content = str_replace("<h5>\r\n", "<h5>", $content); 
    $content = str_replace("</h5>\r\n", "</h5><p>", $content); 
    $content = str_replace("<h6>\r\n", "<h6>", $content); 
    $content = str_replace("</h6>\r\n", "</h6><p>", $content); 
    $content = str_replace("<ul>\r\n", "<ul>", $content); 
    $content = str_replace("</ul>\r\n", "</ul><p>", $content); 
    $content = str_replace("<ol>\r\n", "<ol>", $content); 
    $content = str_replace("</ol>\r\n", "</ol><p>", $content); 
    $content = str_replace("<li>\r\n", "<li>", $content); 
    $content = str_replace("</li>\r\n", "</li>", $content); 
    $content = "<p>" . str_replace("\r\n", "</p><p>", $content); 
    $content = str_replace("<p><h1>", "<h1>", $content); 
    $content = str_replace("<p><h2>", "<h2>", $content); 
    $content = str_replace("<p><h3>", "<h3>", $content); 
    $content = str_replace("<p><h4>", "<h4>", $content); 
    $content = str_replace("<p><h5>", "<h5>", $content); 
    $content = str_replace("<p><h6>", "<h6>", $content); 
    $content = str_replace("<p><ul>", "<ul>", $content); 
    $content = str_replace("<p><ol>", "<ol>", $content); 
    return $content; 
} 

function format_html_end($content) 
{ 
    $content = str_replace("</h1></p>", "</h1>", $content); 
    $content = str_replace("</h2></p>", "</h2>", $content); 
    $content = str_replace("</h3></p>", "</h3>", $content); 
    $content = str_replace("</h4></p>", "</h4>", $content); 
    $content = str_replace("</h5></p>", "</h5>", $content); 
    $content = str_replace("</h6></p>", "</h6>", $content); 
    $content = str_replace("</ul></p>", "</ul>", $content); 
    $content = str_replace("</ol></p>", "</ol>", $content); 
    return $content; 
} 

?> 

<?php 
$con = mysql_connect("localhost","username","password"); 
if (!$con) 
    { 
    die('Could not connect: ' . mysql_error()); 
    } 

mysql_select_db("db", $con); 

$result = mysql_query("SELECT column FROM table WHERE id = '1'"); 

while($row = mysql_fetch_array($result)) 
    { 
    $content = $row['column']; 
    echo format_html_end(format_html("$content</p>")); 
    } 

mysql_close($con); 
?> 

的從表中的內容會是這個樣子......

<h1>Header</h1> 
ertertert 
ertertertert 
rhdfgh 
dfghdfghdfgh 
ddfgh 
<ul> 
<li>fdghdfghd</li> 
<li>fghjfghj</li> 
</ul> 
+0

正則表達式,也許? – tdammers 2012-02-10 16:00:51

+3

你究竟想完成什麼?除非有預標記,否則新行簡單地被視爲空白並與HTML中的其他空白空間緊密結合。 – evan 2012-02-10 16:01:25

+0

您正在刪除比相應的'

'更多的'

'。代碼看起來可怕..我敢肯定,你會遇到與標籤匹配問題 – 2012-02-10 16:05:40

回答

2

你可以用幾個正則表達式來處理幾乎所有的問題:

$content = preg_replace("/<(h[1-6]|ul|ol)>\r\n/", "<$1>", $content); 
$content = preg_replace("/<\/(h[1-6]|ul|ol)>\r\n/", "</$1><p>", $content); 
$content = preg_replace("/<(\/?)li>\r\n/", "<$1li>", $content); 
$content = preg_replace("/<p><(h[1-6]|ul|ol)>/", "<$1>", $content); 
$content = preg_replace("/<\/(h[1-6]|ul|ol)><\/p>/", "</$1>", $content); 

這些技巧是,您可以在進行替換時使用捕獲和反向引用。因此,例如,第一個正則表達式可以匹配h1-h6,ulol,並且在替換期間$1具有與其匹配的那些值中的值。

下面這行代碼會保留原樣,因爲它與其他正則表達式沒有任何共同之處,並且工作正常。

$content = "<p>" . str_replace("\r\n", "</p><p>", $content); 
+0

儘管它在每個列表項目的第二個項目中都有效,但它幾乎可行。它將p標籤添加到第二個和後續列表項中。 – Tom 2012-02-10 16:43:39

+0

我看到...列表項的處理稍有不同。我會修改我的答案。 – Feysal 2012-02-10 17:35:06

0

我不明白爲什麼你需要所有這些替代品,但你可以使用數組與str_replace

3

大概應該是在代碼審查不在這裏,但很好啊:

str_replace函數接受數組,例如:

<?php 

function format_html($content) 
{ 
    $replace = array("<h1>\r\n","</h1>\r\n","<h2>\r\n",...); 
    $with = array("<h1>","</h1>","<h2>\r\n",...); 

    $content = str_replace($replace, $with, $content); 
    return $content; 
} 
0

隨着他們中的很多,你可以這樣做:

$content = str_replace(PHP_EOL, "<p>", $content); 
0

你會想做一個多部分正則表達式。這是我可以快速充實的東西。這將通過使用環視表達式匹配來大大減少代碼量。如果這些是通用標籤規則,請將下面的「」替換爲「<。*>」。

$patterns = array(); 
$patterns[0] = '/(?<=<h[1-6]>)\r\n/'; // removes \r\n after the tag 
$patterns[1] = '/<p>(?=<h[1-6]>)/'; // removes <p> if before the tag 
echo preg_replace($patterns, '', $content); 

幫助上的preg_replace:提前http://www.php.net/manual/en/function.preg-replace.php

外觀和向後看:http://www.regular-expressions.info/refadv.html