Forum Moderators: mack
For example, if you know ASP, you could do this:
<%
content=Request.Form("content")
Response.Write (Replace(content, Chr(13), "<p />"))
%><html>
<head>
</head><body>
<form method=post action=test.asp>
<textarea rows=4 cols=50 name=content></textarea>
<input type=submit value=submit>
</form></body>
</html>
Of course the same thing can be done in PHP, or Perl. The "test.asp" feeds itself the content in the form and writes it out with the Chr(13) (a carriage return) replaced by a <p />.
If you do not know how to write in either of these languages, I'm not sure if there is another way to accomplish it.
Anyone else know of a simpler way?
<%
article=Request.Form("article")
Response.Write (Replace(article, Chr(13), "<p />"))
%>
<html>
<body>
<form action="insert_db.php" method="post">
Title <br />
<input size="50" type="text" name="title" /> <br /><br />
Article <br />
<textarea name="article" cols="50" rows="10"></textarea><br /><br />
<p>
<input type="submit" name="submit" value="Broadcast it!" />
<input type="reset" name="reset" value="Reset" />
</form>
</body>
</html>
Any other advice?
<?php$article=$_POST["article"];
$newarticle=str_replace(Chr(13),'<p>', $article);
print $newarticle;
?><html>
<body><form action="insert_db.php" method="post">
Title <br />
<input size="50" type="text" name="title" /> <br /><br />Article <br />
<textarea name="article" cols="50" rows="10"></textarea><br /><br /><p>
<input type="submit" name="submit" value="Broadcast it!" />
<input type="reset" name="reset" value="Reset" /></form>
</body>
</html>
The string $newarticle is the one with the newly formatted text. Of course, you can rename this to whatever you like.
In other words, you could copy/paste that code into a file called "insert_db.php". This page will load in your browser window with the Title and Article fields.
When you type in content and click "Broadcast It", your content will be written to the screen, with the paragraph distinctions.
Of course, the PHP portion contained at the tope (between <?php and?> can be placed in another PHP file if you want.
You would then change the <form method=post action=someotherfile.php> to point to someotherfile.php.
My ASP is too rusty to exemplify. You need a regexp that looks for a line that is only whitespace. Here's my PERL example. :-)
# ^ = beginning of any line
# $ = ending of any line
# \s+ = 1 or more of any white space character -
# if whitespace is all that's on a line, this includes any line feed
# s/a/b/; If you find a, substitute it for b
$line =~ s/^\s+$/<p>\n/g;
There's actually more to remove multiple lines and include a closing tag, but trying to K.I.S.S.
// Only allow two newlines in a row.
$html = preg_replace("/\n\n+/", "\n\n", $html);
// Put <p>..</p> around paragraphs
$html = preg_replace('/\n?(.+?)(\n\n¦\z)/s', "<p>$1</p>", $html);
// convert newlines not preceded by </p> to a <br /> tag
$html = preg_replace('¦(?<!</p> )\s*\n¦', "<br />", $html);
return $html;
}