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>
3
  • 3
    What is it that you're actually trying to accomplish? New lines are simply viewed as white space and condensed with other white space in HTML unless there's a pre tag. Commented Feb 10, 2012 at 16:01
  • you are removing a lot of <p> than the corresponding </p>. the code looks terrible.. I'm sure you will run into problems with tag matching Commented Feb 10, 2012 at 16:05
  • I'm fairly new to php so not too sure of all the php functions available... hance I'm asking if there's a better way to do this? Commented Feb 10, 2012 at 16:10

5 Answers 5

3

Should probably be on codereview not here, but ah well:

str_replace accepts arrays, for example:

<?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;
 }
Sign up to request clarification or add additional context in comments.

Comments

2

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

2 Comments

This almost works although it falls over at the second of each list items. It adds p tags to the second and subsequent list items.
I see... the handling of list items was slightly different. I will revise my answer.
0

I don't understand why you need all these replacements, but you can use arrays with str_replace

Comments

0

With a lot of them, you can do this:

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

Comments

0

You will want to do a multipart regular expression. Here is something that may work that I quickly fleshed out. This will greatly reduce the amount of code by using look-around expression matching. Replace "" below with "<.*>" if these are a universal tag rules.

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

Help on preg_replace: http://www.php.net/manual/en/function.preg-replace.php

Look ahead and look behind: http://www.regular-expressions.info/refadv.html

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.