I have the following code which works fine to format text from an SQL table. It seems a little long winded though.

It will create paragraphs from the line breaks but ignore header and list tags (not wrap those in "p" tags.
Can anyone see an obvious way to condense this?

<?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);
?>

The content from the table will look something like this...

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

Instead of

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

why don't you simply replace </p> by "".Is there any other tags after which you want </p> tag?

Member Avatar for diafol

str_replace takes arrays as parameters, so you can create a find array and a search array and just use the one str_replace:

$find = array("<h1>\r\n", "</h1>\r\n", "<h2>\r\n", ...);
$search = array("<h1>", "</h1><p>", "<h2>", ...);
$content = str_replace($find, $search, $content);
Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.