Hi everyone, I am new to the forum, and I'm not very knowledgable, so I'll probably do more asking than contributing, and I apologize in advance for that.

My question is about a guestbook I'm using. Most people know it, it's the one from Matt's Script archive. Only, I'm trying to use it as a way for my visitors to post stories to the web page. The only fields being posted really are their name, the name of the story, an intro to the story, the date, and the story itself.

My problem comes when you submit a large story, any longer than one paragraph. There is no paragraph filtering in the code, so if there is more than one paragraph in their story, the code sticks it all together, and it gets posted on the web page in one extremely large paragraph. Totally unreadable.

I figured that I had to put something in the code to find all the carraige returns, (which I guess are represented by "\n" in perl), and have the code replace all those with HTML-recognizable paragraph symbols like "<p>" or something like that. I understand the concept, but don't know how to do it.

This guestbook was my first introduction to the Perl language. I have a small understanding of it now, but need help figuring out how to implement this. Any ideas?

Thanks,
Nate
p.s. Sorry I'm a little long winded.

It's ok, I'm pretty long winded myself!

Welcome to daniweb, and I'm sure you'll be great addition to the community here. Especially if you're coding Perl! :D

What you need to use to solve your problem is called "regex" or "regular expressions". It's very easy to use, but it looks really complicated. Also, regex can be as simple as just matching to see if a string exists within another string, to something as complex as splitting strings into different variables. What you need, to accomplish your task, is called "substitution". Let me give you an example. Say for instance that you have, a variable called $story. This variable will contain paragraphs of data. In order to substitute say \n for <BR>, you could do something like this:

$story =~ s/\n/<BR>/gi;

Let me give you a minor breakdown, without making this a tutorial. $story is your variable, the =~ (without going into bitwise reasoning) says apply the stuff on the right to the variable on the left. You have to tell substitute what variable to work with... =~ is how you do it. The 's' is what lets perl know to use "substitution". The first and second slashes / encase the character to be replaced, and the second and third slashes / encase the character(s) to be replaced with. So, in some kind of english terms, the above regex code would say "use $story, and replace all the \n's with <BR>'s". The 'g' at the end of the regex tells perl to make the search and replace global for the entire string.... so not to stop at the first or second replace. The 'i' that follows it, means to ignore case. So it won't distinguish between 'A' and 'a'. This doesn't really apply to the newline character, but I wanted to throw it in for your knowledge (cause it's used a lot).

I hope my long winded explanation has helped you a little. If you are still having trouble with it, post the code (and please, enclose the code in code tags), and I'll gladly have a look.

Thanks for the reply! I had found that little code snippet somewhere else, and thought that might be the one I need to use. You have confirmed that. Now I have to implement it. My perl code grabs the information from my form that posts to it. Say the field where the person is pasting their story is named 'story'. Do I want to use this code when it is being pulled from the form, or when it is putting it to the html page? I would think it would be easer to do it when it is posting to the web page. Here is an example of the code I have that will be pringting it to the HTML page.

open (GUEST,">$guestbookreal") || die "Can't Open $guestbookreal: $!\n";

for ($i=0;$i<=$SIZE;$i++) {
   $_=$LINES[$i];
   if (/<!--begin-->/) { 

     if ($entry_order eq '1') {
         print GUEST "<!--begin-->\n";
      }
   
     
         print GUEST "$FORM{'realname'}<br>";
      
         print GUEST "$FORM{'username'}";
         
         print GUEST "$FORM{'story'}";

      print GUEST "<br>\n";
      }
close (GUEST);

That code may not work, because I changed it and erased some stuff to make it easier to read in this post. But you can see where it calls the form field, 'story', and prints it to the page. Would I put it inside that tag, or up at the begining of the whole code? Do I need to assign a variable to that form field, so that it can be called from that code?
Thanks for your help.
Nate

I would do it when it loads the HTML page to the browser. You can have it do it once it reads it from the form, and have it save it in the file that way, but I like to do most of my formatting on the display side. This gives me the ability to store the data any way I want to.... and then just show it the way the browser wants it.

This is what I would do:

open (GUEST,">$guestbookreal") || die "Can't Open $guestbookreal: $!\n";

for ($i=0;$i<=$SIZE;$i++) {
   $_=$LINES[$i];
   if (/<!--begin-->/) { 

     if ($entry_order eq '1') {
         print GUEST "<!--begin-->\n";
      }
   
     
         print GUEST "$FORM{'realname'}<br>";
      
         print GUEST "$FORM{'username'}";
         

         # /* I Added The Following 2 Lines */
         $story = $FORM{'story'};
         $story =~ s/\n/<BR>/gi;

         # /* I Modified This Line */
         print GUEST "$story";

         print GUEST "<br>\n";
      }
close (GUEST);

I Added Comments where I modifed/added to the script. Let me know your thoughts.

ps: I think you could have left the $story variable out of it, and ran the regex on the hash (associative array), but this allows you to keep the original data read from the file unchanged.... and then must use a new variable for the changes....

Thanks so much! You've made this really easy. I'm going to try it out right now.
Thanks,
Nate

Not that it's necessary to change something that works (believing this may be a perl sin, so i'll say a perl prayer later...) but you can change those two lines into one and eliminate the extra variable:

$FORM{'story'} =~ s/\n/<BR>/gi;

Kordaff

Not that it's necessary to change something that works (believing this may be a perl sin, so i'll say a perl prayer later...) but you can change those two lines into one and eliminate the extra variable:

$FORM{'story'} =~ s/\n/<BR>/gi;

Kordaff

Yes Indeed, and in my previous post:

ps: I think you could have left the $story variable out of it, and ran the regex on the hash (associative array), but this allows you to keep the original data read from the file unchanged.... and then must use a new variable for the changes....

This article has been dead for over six months. Start a new discussion instead.