Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 7488211
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 29, 20262026-05-29T14:47:39+00:00 2026-05-29T14:47:39+00:00

I have the following code which works fine to format text from an SQL

  • 0

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>
  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-29T14:47:42+00:00Added an answer on May 29, 2026 at 2:47 pm

    You can deal with nearly all of that with some regular expressions:

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

    The trick with these is that you can use capturing and backward references when doing the replacement. So for example the first regexp can match h1-h6, ul or ol, and during replacing $1 has the value of whichever of those it matched.

    The following line of code I would leave as is, since it doesn’t have anything in common with the other regular expressions, and works fine.

    $content = "<p>" . str_replace("\r\n", "</p><p>", $content);
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have the following code which works fine. However, I only want to return
At the moment I have the following code which works fine. label = new
I have the following code which works just fine when the method is POST,
I have the following code for Android which works fine to play a sound
I have code that looks like the following, which works fine for displaying the
I have the following PHP code which works out the possible combinations from a
I have following code which works fine in firefox and ie 8, but in
I have the following piece of code which works fine and does what it
I have the following jQuery code which all works fine: $(#sidebar .m3, #sidebar .m5).toggle(function()
I have the following code to retrieve a file using FTP (which works fine).

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.