PHP mysql格式化文本

标签 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>

最佳答案

可能应该在 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;
 }

关于PHP mysql格式化文本,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/9230687/

相关文章:

php - 在连接查询中查找最近的记录

mysql - 我想获取像 52,100 这样的值,但它只返回52 mysql codeigniter

php - 通过外部脚本插入 Wordpress 帖子

c# - 如何: Get text index from label by mouse position? C#

javascript - 使用JQuery获取div中未选中复选框的值

PHP MySQL : Search query work only with existing full term

php - Laravel 5.2.31 中 API 的 TokenMismatchException

php - MYSQL中Case语句中AND的使用

database - 最好将数据存储在 RAM、文本文件或数据库中

python - 如何阻止 BERT 将特定单词分解成词 block