2012-02-10 7 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

正規表現:http://www.php.net/manual/en/function.preg-replace.php

先に見て、背後に見えますか? – tdammers

+3

実際に達成しようとしていることは何ですか?新しい行は、プレタグがない限り、単純に空白として表示され、HTMLの他の空白と圧縮されます。 – evan

+0

対応する「

」よりも多くの「

」を削除しています。コードはひどいです..私はあなたがタグマッチングの問題に遭遇すると確信しています –

答えて

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-h6ulまたはolに一致し、置換時には$1のいずれかの値が一致します。

次のコード行は、他の正規表現と共通するものはないため、そのまま使用しても問題ありません。

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

これはほとんどの作品は、各リスト項目の2番目に転倒します。 2番目以降のリスト項目にpタグを追加します。 – Tom

+0

私は見る...リスト項目の処理は若干異なっていた。私は私の答えを改めるつもりです。 – Feysal

0

私はあなたがすべてのこれらの置換を必要とする理由を理解していないが、あなたはで配列を使用することができます...このようになります。

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.regular-expressions.info/refadv.html

関連する問題