473,394 Members | 1,721 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,394 software developers and data experts.

Script Bug - Inserts spaces after every return character

{NOTE . = space}
I made a script to allow me to edit my webpages online through a text area in my cgi script but I've run into a problem. For whatever reason the text area seems to put spaces in front of each line {except for the first} so,
line one
line two
line three

becomes
line one
.line two
.line three
next time it becomes
line one
..line two
..line three

eventually it winds up being like
line one
.......................................line two
.......................................line three

Its made a mess of my files.
Is there a way to process the input from the text area to remove all of the spaces before the start of the lines?
I'm hoping that I can get it worked out so while using the script my pages will wind up like it was to start with.
Thanks, any help would be greatly appreciated :)
John
Jul 19 '07 #1
4 2271
numberwhun
3,509 Expert Mod 2GB
Have you tried anything so far to do it? My hint would be to use a substitution regular expression, something along the lines of the following:

s/^\s+//

I leave it in your hands to pass each line through it and see if it works. Let us know if you need more assistance. If you do (and in the future), please be sure and post your code when asking for help. :-)

Regards,

Jeff
Jul 19 '07 #2
KevinADC
4,059 Expert 2GB
textareas do not magically put in extra spaces. You can of course use a regexp like Jeff posted to work around your problem. But the real problem is elsewhere, most likely in the function you use to initially process the form data or when you print it to the file.
Jul 19 '07 #3
I haven't tried anything to fix the problem yet as I'm not really sure how to go about it and you can probably tell from the code below that I'm not real good at writing cgi. I get by but thats about it. Part of the problem may be that the cgi script doesn't start the files, they are normally written using coffee cup or note pad then this script is used for maintaining the pages.
Anyway heres the script its just a little over 100 lines so I posted the whole thing. To run it make a test html file with a link to the cgi script in the file. the password is pass

Expand|Select|Wrap|Line Numbers
  1. #!/usr/bin/perl
  2.  
  3. #Setup system variables
  4.  
  5. for ($i = 0; $i < 2; $i++){
  6.     ($i eq 0) ? ($f=<stdin>) : ($f=$ENV{QUERY_STRING});
  7.     @f = split(/\&/, $f);
  8.     foreach $l (@f) {
  9.         ($n,$v) = split(/\=/, $l);
  10.         $v =~ s|\+| |g;
  11.         $v =~ s|%([a-fA-F0-9][a-fA-F0-9])|pack("C", hex($1))|ge;
  12.         $form{$n}=$v;
  13.     }
  14. }
  15.  
  16. $book = $ENV{'HTTP_REFERER'};
  17. @sbook = [split(/\b/, $book)];
  18. $a=0;
  19. while ($sbook[0][$a] ne "") {
  20.     if ($sbook[0][$a] eq "com"){
  21.         $b = $a;
  22.     }
  23.     $a++;
  24. }
  25. $a = $b+2;
  26. while ($sbook[0][$a] ne "") {
  27.     $filename=$filename.$sbook[0][$a];
  28.     $a++;
  29. }
  30. $c = $a-3;
  31. $back = $sbook[0][$c];
  32. $c++;
  33. $back = $back.$sbook[0][$c];
  34. $c++;
  35. $back = $back.$sbook[0][$c];
  36.  
  37. if ($form{ACTION} eq "save") {
  38.     &SAVE;
  39.  
  40. } elsif ($form{ACTION} eq "view") {
  41.     &VIEW;
  42.  
  43. } else {
  44.     &LOGIN;
  45. }
  46.  
  47. sub LOGIN {
  48.     print "Content-type:text/html\n\n";
  49.     print qq~
  50. <center>
  51. <form action="$ENV{SCRIPT_NAME}?ACTION=view" method="post">
  52. <input type="text" name="password" value=""><BR>
  53. <input type="hidden" name="fname" value="$filename">
  54. <input type="hidden" name="bname" value="$back">
  55. <input type="submit" value=" ">
  56. </form>
  57. <a href=$ENV{'HTTP_REFERER'}>Cancel</a>
  58. ~;
  59. }
  60.  
  61. sub VIEW {
  62.     $fname = $form{fname};
  63.     $bname = $form{bname};
  64.     if ($form{password} ne "pass") {
  65.         die;
  66.     }
  67.     open(MESSAGE, "../$fname");
  68.     my @temp = <MESSAGE>;
  69.     close(MESSAGE);
  70.     print "Content-type:text/html\n\n";
  71.     print qq~
  72. <center>
  73. <form action="$ENV{SCRIPT_NAME}?ACTION=save" method="post">
  74. <input type="hidden" name="password" value="$form{password}">
  75. <textarea name="content" rows="20" cols="90">@temp</textarea>
  76. <input type="hidden" name="fname" value="$fname">
  77. <input type="hidden" name="bname" value="$bname">
  78. <BR><input type="submit" value="Save Changes">
  79. </form>
  80. <!--<a href=$ENV{'HTTP_REFERER'}>Cancel</a>
  81. <form action="$ENV{SCRIPT_NAME}?ACTION=edcon" method="post">
  82. <input type="hidden" name="password" value="$form{password}">
  83. <input type="submit" value="Edit Converter">
  84. </form>-->
  85. ~;
  86. }
  87.  
  88.  
  89. sub SAVE {
  90.     $fname=$form{fname};
  91.     $bname=$form{bname};
  92.     if ($form{password} ne "pass") {
  93.         die;
  94.     }
  95.     #get date and create backup
  96.     $a = localtime;
  97.     $g = substr($a,4,3); # month
  98.     $d = substr($a, -4); # year
  99.     $c = substr($a,8,2); # Day
  100.     $e = substr($a,11,2); # hour
  101.     $f = substr($a,14,2); # min
  102.     if ($c<10) {
  103.         $c = "0".substr($c,1);
  104.     } # put 0 in front 03 05 etc
  105.     $date = $g."-".$c."-".$d."---".$e."-".$f;
  106.     open(MESSAGE, "../$fname");
  107.     my @temp = <MESSAGE>;
  108.     close(MESSAGE);
  109.     open(MESSAGE, ">../backup/$date$bname");
  110.     print MESSAGE "@temp";
  111.     close(MESSAGE);
  112.     #endbackup
  113.  
  114.     open(MESSAGE, ">../$fname");
  115.     print MESSAGE "$form{content}";
  116.     close(MESSAGE);
  117.     print "Content-type:text/html\n\n";
  118.     print qq~
  119. <center>
  120. Changes saved<BR>
  121. <a href="$fname">go to page</a>
  122. ~;
  123. }
  124.  
Jul 19 '07 #4
KevinADC
4,059 Expert 2GB
I can't tell what might be the problem just by looking at the code. BTW, the code is really quite bad. I urge you to not use it at all.
Jul 20 '07 #5

Sign in to post your reply or Sign up for a free account.

Similar topics

5
by: Stu Cazzo | last post by:
I have the following: String myStringArray; String myString = "98 99 100"; I want to split up myString and put it into myStringArray. If I use this: myStringArray = myString.split(" "); it...
7
by: Lowell Kirsh | last post by:
I have a script which I use to find all duplicates of files within a given directory and all its subdirectories. It seems like it's longer than it needs to be but I can't figure out how to shorten...
5
by: Jonathan Ng | last post by:
Hi, I was wondering if there was a way to include the white spaces in a string. Currently, I am using: scanf("%s", &input); However, this doesn't include the 'space' character or any other...
9
by: Durgesh Sharma | last post by:
Hi All, Pleas help me .I am a starter as far as C Language is concerned . How can i Right Trim all the white spaces of a very long (2000 chars) Charecter string ( from the Right Side ) ? or how...
135
by: Xah Lee | last post by:
Tabs versus Spaces in Source Code Xah Lee, 2006-05-13 In coding a computer program, there's often the choices of tabs or spaces for code indentation. There is a large amount of confusion about...
40
by: raphfrk | last post by:
I have a program which reads in 3 filenames from the command line prog filename1 filename2 filename3 However, it doesn't work when one of the filenames has spaces in it (due to a directory...
9
by: Synapse Syndrome | last post by:
Hi I've been given what I am told is a PHP script to be used on my server. I do not know any PHP. I am trying to use a feature of a program called ArchiCAD. This feauture allows CAD drawing...
15
by: DanielJohnson | last post by:
I am writing a program in which I am removing all the spaces from the string. I thought that I could do it two ways. One was parsing the string character by character and copying onto another...
8
by: need2know2 | last post by:
I'm filling an array with explode(";" $linetoread) and then using the array to write to a file. When I open the file the array elements have spaces between each character. I've tried split as well...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.