PHP mysql格式化文本

时间:2012-02-10 15:58:54

标签: php mysql text formatting

我有以下代码可以很好地格式化SQL表格中的文本。虽然看起来有点啰嗦。

它将从换行符创建段落,但忽略标题和列表标记(不包括“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>

5 个答案:

答案 0 :(得分:3)

可能应该在codereview上,而不是在这里,但是好啊:

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;
 }

答案 1 :(得分: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-h6ulol,并且在替换期间$1具有匹配的值中的任何一个。

我将保留以下代码行,因为它与其他正则表达式没有任何共同点,并且工作正常。

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

答案 2 :(得分:0)

我不明白为什么你需要所有这些替换,但你可以使用str_replace

的数组

答案 3 :(得分:0)

有很多,你可以这样做:

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

答案 4 :(得分:0)

您需要执行多部分正则表达式。这是可以工作的东西,我很快就充实了。这将通过使用环视表达式匹配大大减少代码量。将“&lt;。*&gt;”替换为下面的“”如果这些是通用标签规则。

$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