CGI/Perl Guide | Learning Center | Forums | Advertise | Login
Site Search: in

  Main Index MAIN
INDEX
Search Posts SEARCH
POSTS
Who's Online WHO'S
ONLINE
Log in LOG
IN

Home: Perl Programming Help: Regular Expressions:
HTML code generator

 



inlimbo
User

Jan 29, 2005, 6:50 PM

Post #1 of 2 (2394 views)
HTML code generator Can't Post

I want to create a little script that adds the <p>,</p> and <br> tags to some text. i.e. If I have the following text:

my $text = "This is paragraph 1. This is paragraph 1. This is paragraph 1. This is paragraph 1. This is paragraph 1. This is paragraph 1. This is paragraph 1. This is paragraph 1. This is paragraph 1. This is paragraph 1. This is paragraph 1. This is paragraph 1. This is paragraph 1.

This is paragraph 2. This is paragraph 2. This is paragraph 2. This is paragraph 2. This is paragraph 2. This is paragraph 2. This is paragraph 2. This is paragraph 2. This is paragraph 2. This is paragraph 2. This is paragraph 2. This is paragraph 2. This is paragraph 2. This is paragraph 2.
This is a new line.
This is a new line.
This is a new line.
This is a new line.";

I want it to come out as:


Code
my $text = "<p>This is paragraph 1. This is paragraph 1. This is paragraph 1. This is paragraph 1. This 
is paragraph 1. This is paragraph 1. This is paragraph 1. This is paragraph 1.
This is paragraph 1. This is paragraph 1. This is paragraph 1. This is paragraph
1. This is paragraph 1.</p>
<p>This is paragraph 2. This is paragraph 2. This is paragraph 2. This is paragraph
2. This is paragraph 2. This is paragraph 2. This is paragraph 2. This is
paragraph 2. This is paragraph 2. This is paragraph 2. This is paragraph 2. This
is paragraph 2. This is paragraph 2. This is paragraph 2.<br>
This is a new line.<br>
This is a new line.<br>
This is a new line.<br>
This is a new line.</p>"


ie. just put the <p> and </p> at the beginning and end of the paragraph and put a <br> tag if there is a new line.

Thanx


davorg
Thaumaturge / Moderator

Feb 2, 2005, 2:01 AM

Post #2 of 2 (2371 views)
Re: [inlimbo] HTML code generator [In reply to] Can't Post

This seems to do the job on your text data.


Code
#!/usr/bin/perl 

use strict;
use warnings;

my $text = 'This is paragraph 1. This is paragraph 1. This is paragraph 1. This is paragraph 1. This is paragraph 1. This is paragraph 1. This is paragraph 1. This is paragraph 1. This is paragraph 1. This is paragraph 1. This is paragraph 1. This is paragraph 1. This is paragraph 1.

This is paragraph 2. This is paragraph 2. This is paragraph 2. This is paragraph 2. This is paragraph 2. This is paragraph 2. This is paragraph 2. This is paragraph 2. This is paragraph 2. This is paragraph 2. This is paragraph 2. This is paragraph 2. This is paragraph 2. This is paragraph 2.
This is a new line.
This is a new line.
This is a new line.
This is a new line.';

# Add <p> and </p> to start and end of text
$text = "<p>$text</p>";

# Convert double newlines to </p>\n\n</p>
$text =~ s|\n\n|</p>\n\n<p>|g;

# Add a <br /> at the end of any non-blank line that doesn't
# already end with </p>
$text =~ s|\S(?<!</p>)\n|<br />\n|g;

print $text;


--
Dave Cross, Perl Hacker, Trainer and Writer
http://www.dave.org.uk/
Get more help at Perl Monks

 
 


Search for (options) Powered by Gossamer Forum v.1.2.0

Web Applications & Managed Hosting Powered by Gossamer Threads
Visit our Mailing List Archives