LinuxQuestions.org
Share your knowledge at the LQ Wiki.
Home Forums Tutorials Articles Register
Go Back   LinuxQuestions.org > Forums > Non-*NIX Forums > Programming
User Name
Password
Programming This forum is for all programming questions.
The question does not have to be directly related to Linux and any language is fair game.

Notices


Reply
  Search this Thread
Old 12-09-2011, 09:20 PM   #1
errigour
Member
 
Registered: May 2009
Posts: 366

Rep: Reputation: 6
Formatting a string for my guestbook


Hey I was wondering if someone here that is good at
working with strings would help me create
guestbook.php for me. I have a string $comments that
I want to write to a file but I want to write it to a
file in such a way that each line of text written to
the file is less then 50 characters long. so that the
file wont have a single line 900 characters long.
I'm not very good with stuff like this but if someone
helps me out I'll have something to look back on as
a reference. I would really like to see some cool php
code also. Also I'll post the complete guestbook.php
code to if anyone would like to see it and I'll post
my web page if anyone wants to sign my guest book.
 
Old 12-10-2011, 04:36 AM   #2
lithos
Senior Member
 
Registered: Jan 2010
Location: SI : 45.9531, 15.4894
Distribution: CentOS, OpenNA/Trustix, testing desktop openSuse 12.1 /Cinnamon/KDE4.8
Posts: 1,144

Rep: Reputation: 217Reputation: 217Reputation: 217
Here is what I would do (a quick and not complete solution because it doesn't write to file, just shows on screen)
Code:
<?PHP

$Settings['Log']="logs/guestbooklog.txt"; // path to log file
$comments="Formatting a string for my guestbook
-------------
Hey I was wondering if someone here that is good at
working with strings would help me create
guestbook.php for me. I have a string $comments that
I want to write to a file but I want to write it to a
file in such a way that each line of text written to
the file is less then 50 characters long. so that the
file wont have a single line 900 characters long.
I'm not very good with stuff like this but if someone
helps me out I'll have something to look back on as
a reference. I would really like to see some cool php
code also. Also I'll post the complete guestbook.php
code to if anyone would like to see it and I'll post
my web page if anyone wants to sign my guest book.";

//	if ( (file_exists($Settings['Log'])) && (is_writable($Settings['Log'])) ) {
			$fcontents.="\n".'Date:   '. date("M d, Y").' , '.date("G:i").'h'."\n\n<br>";    //  write Date and Hour to line of contents for file
            $chrlen= strlen($comments);   // get the length of $comments string
			echo "Length of comments: ".$chrlen."<br>";
			$i=0;
			$lengthofline=50;
			while ($i <= $chrlen) {
				$fcontents.=substr($comments,$i,$lengthofline)."\n<br>";   // return $lengthofline characters and make new line
				$i=$i+$lengthofline;
			}  // while loop
			$fcontents.="$$$$$$$$$$$$$$$$$\n<br>";
			echo "File contents:\n<br>".$fcontents;
//    }  // if file exists & is_writable
?>
Now to write to a file you have to remove the "<br>" and make a call to fopen and fwrite to write to a file.

The output would be:
Code:
Length of comments: 716
File contents:
Date: Dec 10, 2011 , 11:36h
Formatting a string for my guestbook -------------
Hey I was wondering if someone here that is good
at working with strings would help me create guest
book.php for me. I have a string that I want to w
rite to a file but I want to write it to a file in
such a way that each line of text written to the
file is less then 50 characters long. so that the
file wont have a single line 900 characters long.
I'm not very good with stuff like this but if some
one helps me out I'll have something to look back
on as a reference. I would really like to see some
cool php code also. Also I'll post the complete g
uestbook.php code to if anyone would like to see i
t and I'll post my web page if anyone wants to sig
n my guest book.
$$$$$$$$$$$$$$$$$
The 'function' needs more improvements (maybe to search for ' ' spaces and delimit words and more) but it's a working start for you to continue.

good luck

Last edited by lithos; 12-10-2011 at 04:38 AM.
 
Old 12-10-2011, 04:39 AM   #3
Proud
Senior Member
 
Registered: Dec 2002
Location: England
Distribution: Used to use Mandrake/Mandriva
Posts: 2,794

Rep: Reputation: 116Reputation: 116
Post the bit of your code inside [code][/code] or [php][/php] tags, and tell us where you're stuck modifying it. We'll help you learn to fix it yourself, please don't just post or ask for the whole thing.

Edit: lithos has give one starting example, or stick to the guestbook code you said you've already been looking at.

Last edited by Proud; 12-10-2011 at 04:41 AM.
 
Old 12-10-2011, 06:10 AM   #4
j-ray
Senior Member
 
Registered: Jan 2002
Location: germany
Distribution: ubuntu, mint, suse
Posts: 1,591

Rep: Reputation: 145Reputation: 145
You can split the string into an array with explode(' ', $string)

then run in a foreach loop thru the array in which you add the new word/element to the resulting string. If the resulting string length is beyond a given limit you add a newline or <br >/ tag before adding the element.

Then print the resulting string into the log file.

This way you don't cut words as you would if you only take strlen() to create the new lines.
 
Old 12-10-2011, 01:32 PM   #5
errigour
Member
 
Registered: May 2009
Posts: 366

Original Poster
Rep: Reputation: 6
Alright here is the file. Its still not complete because it writes hard returns
as well as word wrapping every 50 characters. There are some include files that will
keep the php file from being tested and you can delete the last include on the
bottem of the file completly if you want to test the program. Below the php
code is the guestbook.html file. and below that is the segment of code I am working
with.

PHP Code:
<?php
if (!isset($_POST["name"]) || !$_POST["name"])
{
    echo 
"<p style=\"color:red;font-weight:bold;margin-left:95px\">";
    echo 
"* A name is required</p>";
    include(
"guestbook.html");
    exit;

else 
{
    
$server   $_SERVER['REMOTE_ADDR'];
    
$name     $_POST["name"];
    
$homepage $_POST["homepage"];
    
$email    $_POST["email"];
    
$comments $_POST["comments"];
       if (!
$syslog fopen("SYSTEM""a"))
       {
        
$subject "fopen(\"SYSTEM\", \"a\") failure";
        
$header  "SYSTEM LOG FAILURE";
        
$message "FOPEN FAILED TO OPEN SYSTEM: ".date("F j, Y, g: i a");
        echo 
"<p style=\"color:red;font-weight:bold;margin-left;95px\">";
           echo 
"An internal system error has occured.</p>";
           
mail('eric_justin_allan@cfl.rr.com',$subject,$message,$header);
           include(
"guestbook.html");
           exit;
    }
    if(!
$handle fopen("GUESTBOOK","a"))
    {
        
fputs($syslog$server." was unable to open GUESTBOOK ".date("F j, Y, g: i a")."\n");
        
fclose($syslog);
        echo 
"<p style=\"color:red;font-weight:bold;margin-left;95px\">";
        echo 
"Unable to open the guestbook.</p>";
        include(
"guestbook.html");
        exit;
    }
    if(
$_POST["name"])    {fputs($handle$name."\n");    }else{fputs($handle"\n");}
    if(
$_POST["homepage"]){fputs($handle$homepage."\n");}else{fputs($handle"\n");}
    if(
$_POST["email"])   {fputs($handle$email."\n");   }else{fputs($handle"\n");}
    if ( (
file_exists("GUESTBOOK")) && (is_writable("GUESTBOOK")) ) 
    {
            
/* Get the length of $comments string */
            
$chrlenstrlen($comments);
            
$i=0;
            
$lengthofline=50;
            while (
$i <= $chrlen
            {
                
$fcontents.=substr($comments,$i,$lengthofline)."\n";
                
$i=$i+$lengthofline;
            }
//End while loop
            
$fcontents.="$$$$$$$$$$$$$$$$$\n";
            
fputs($handle$fcontents);
    }  
// End if file exists & is_writable
    
fclose($handle);
    include(
"redirect_to_guestbook.php");
}
Code:
<html>
<head>
<title>A guest book for science</title>
<style type="text/css">
body 
{
	background-image: url('jpg/200829053409-8159.jpg');
	color:blue;
	font-family:monospace;
}

a 
{
	text-decoration:none;
    color:white;
}
.t0r0 {position:absolute;
       top:0;
       right:0;}
</style>
</head>





<body>
<a href="index.html">Home page</a>
<form action="guestbook.php" method="post">
	<font color="red">*</font>
	Name,<BR>
	&nbsp; <input name="name" type="text" size="25"><br>
	&nbsp; Homepage,<BR>
	&nbsp; <input name="homepage" type="text" size="25"><br>
	&nbsp; E-mail address,<BR>
	&nbsp; <input name="email" type="text" size="25"><br>
	<BR>
	&nbsp; Comments for the guest book,<br>
	&nbsp; <textarea name="comments" rows="7" cols="47"></textarea>
	<BR>
	&nbsp; <input type="reset"  value="Erase and start over">
	<input type="submit" value="Click here to submit">
</form>
</body>
</html>
<!--embed src="entrance.wav" hidden="true" autostart="true"></embed-->
PHP Code:
    if ( (file_exists("GUESTBOOK")) && (is_writable("GUESTBOOK")) ) 
    {
            
/* Get the length of $comments string */
            
$chrlenstrlen($comments);
            
$i=0;
            
$lengthofline=50;
            while (
$i <= $chrlen
            {
                
$fcontents.=substr($comments,$i,$lengthofline)."\n";
                
$i=$i+$lengthofline;
            }
//End while loop
            
$fcontents.="$$$$$$$$$$$$$$$$$\n";
            
fputs($handle$fcontents);
    }  
// End if file exists & is_writable 
 
Old 12-10-2011, 03:37 PM   #6
Cedrik
Senior Member
 
Registered: Jul 2004
Distribution: Slackware
Posts: 2,140

Rep: Reputation: 244Reputation: 244Reputation: 244
Nice code, although there is a php function for this sort of thing:

PHP Code:
string wordwrap string $str, [OPTIONS...]) 
http://php.net/manual/en/function.wordwrap.php
 
Old 12-10-2011, 03:46 PM   #7
errigour
Member
 
Registered: May 2009
Posts: 366

Original Poster
Rep: Reputation: 6
I just stumbled across that but it still has the same problem as the above code. It doesn't
word wrap lines right because if there is text that someone pressed return while typing,
the output will include the returns also.

Is there a function to remove everything accept white spaces and text?

Last edited by errigour; 12-10-2011 at 03:51 PM.
 
Old 12-10-2011, 03:49 PM   #8
errigour
Member
 
Registered: May 2009
Posts: 366

Original Poster
Rep: Reputation: 6
Woops I didn't mean to make another post

Last edited by errigour; 12-10-2011 at 03:51 PM.
 
Old 12-10-2011, 04:56 PM   #9
Cedrik
Senior Member
 
Registered: Jul 2004
Distribution: Slackware
Posts: 2,140

Rep: Reputation: 244Reputation: 244Reputation: 244
Maybe :
PHP Code:
 if ((file_exists("GUESTBOOK")) && (is_writable("GUESTBOOK")) ) 
    {
            
$comments   str_replace("\n"" "$comments); 
            
$fcontents  wordwrap($comments50);
            
$fcontents .= "$$$$$$$$$$$$$$$$$\n";
            
fputs($handle$fcontents);
    }  
// End if file exists & is_writable 

Last edited by Cedrik; 12-10-2011 at 05:04 PM.
 
Old 12-10-2011, 06:26 PM   #10
errigour
Member
 
Registered: May 2009
Posts: 366

Original Poster
Rep: Reputation: 6
Alright a finished cool php GUESTBOOK

Alright I finally got it to work. I had to add the line,
$comments = str_replace("\r", "", $comments);
just so thzt my file didn't have squares inside the text.
The first file is called guestbook.php
the second is guestbook.html
and the third should be called redirect_to_guestbook.php

My webpage at the moment is o0oo0.net16.net sign my
guestbook if you want. and thanks for all the help.
Also post some cool php code here.


PHP Code:
<?php
if (!isset($_POST["name"]) || !$_POST["name"])
{
    echo 
"<p style=\"color:red;font-weight:bold;margin-left:95px\">";
    echo 
"* A name is required</p>";
    include(
"guestbook.html");
    exit;

else 
{
    
$server   $_SERVER['REMOTE_ADDR'];
    
$name     $_POST["name"];
    
$name ucfirst($name);
    
$homepage $_POST["homepage"];
    
$email    $_POST["email"];
    
$comments $_POST["comments"];
       if (!
$syslog fopen("SYSTEM""a"))
       {
        
$subject "fopen(\"SYSTEM\", \"a\") failure";
        
$header  "SYSTEM LOG FAILURE";
        
$message "FOPEN FAILED TO OPEN SYSTEM: ".date("F j, Y, g: i a");
        echo 
"<p style=\"color:red;font-weight:bold;margin-left;95px\">";
           echo 
"An internal system error has occured.</p>";
           
mail('eric_justin_allan@cfl.rr.com',$subject,$message,$header);
           include(
"guestbook.html");
           exit;
    }
    if(!
$handle fopen("GUESTBOOK","a"))
    {
        
fputs($syslog$server." was unable to open GUESTBOOK ".date("F j, Y, g: i a")."\n");
        
fclose($syslog);
        echo 
"<p style=\"color:red;font-weight:bold;margin-left;95px\">";
        echo 
"Unable to open the guestbook.</p>";
        include(
"guestbook.html");
        exit;
    }
    if(
$_POST["name"])    {fputs($handle$name."\n");    }else{fputs($handle"\n");}
    if(
$_POST["homepage"]){fputs($handle$homepage."\n");}else{fputs($handle"\n");}
    if(
$_POST["email"])   {fputs($handle$email."\n");   }else{fputs($handle"\n");}
    
$comments   str_replace("\n"""$comments);
    
$comments   str_replace("\r"""$comments);
    
$fcontents  wordwrap($comments50);
    
$fcontents .= "\n$$$$$$$$$$$$$$$$$\n";
    
fputs($handle$fcontents."\n");
    
fclose($handle);
    include(
"redirect_to_guestbook.php");
}
?>
Code:
<html>
<head>
<title>A guest book for science</title>
<style type="text/css">
body 
{
	background-image: url('jpg/200829053409-8159.jpg');
	color:blue;
	font-family:monospace;
}

a 
{
	text-decoration:none;
    color:white;
}
.t0r0 {position:absolute;
       top:0;
       right:0;}
</style>
</head>





<body>
<a href="index.html">Home page</a>
<form action="guestbook.php" method="post">
	<font color="red">*</font>
	Name,<BR>
	&nbsp; <input name="name" type="text" size="25"><br>
	&nbsp; Homepage,<BR>
	&nbsp; <input name="homepage" type="text" size="25"><br>
	&nbsp; E-mail address,<BR>
	&nbsp; <input name="email" type="text" size="25"><br>
	<BR>
	&nbsp; Comments for the guest book,<br>
	&nbsp; <textarea name="comments" rows="7" cols="47"></textarea>
	<BR>
	&nbsp; <input type="reset"  value="Erase and start over">
	<input type="submit" value="Click here to submit">
</form>
</body>
</html>
Code:
<?php header( 'Location: /guestbook.html' );?>
Again thanks alot!

Last edited by errigour; 12-11-2011 at 08:05 AM.
 
Old 12-11-2011, 05:03 AM   #11
lithos
Senior Member
 
Registered: Jan 2010
Location: SI : 45.9531, 15.4894
Distribution: CentOS, OpenNA/Trustix, testing desktop openSuse 12.1 /Cinnamon/KDE4.8
Posts: 1,144

Rep: Reputation: 217Reputation: 217Reputation: 217
Nice to see you got it working,
If this is working for you as you wanted, can you mark the thread solved (read in my signature)

Thank you for posting back the solution.
 
  


Reply



Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off



Similar Threads
Thread Thread Starter Forum Replies Last Post
Formatting a datetime string(in g++) fsshl Programming 1 07-11-2011 06:38 PM
squid log string formatting tr3s Linux - Server 4 03-21-2008 04:15 AM
c++ - How to handle user specified string formatting? BrianK Programming 5 02-27-2008 08:14 AM
how to make a guestbook k1ll3r_x Linux - Software 16 07-09-2005 05:04 PM
PHP Guestbook Linuxidiot Programming 1 04-17-2005 09:53 AM

LinuxQuestions.org > Forums > Non-*NIX Forums > Programming

All times are GMT -5. The time now is 12:27 AM.

Main Menu
Advertisement
My LQ
Write for LQ
LinuxQuestions.org is looking for people interested in writing Editorials, Articles, Reviews, and more. If you'd like to contribute content, let us know.
Main Menu
Syndicate
RSS1  Latest Threads
RSS1  LQ News
Twitter: @linuxquestions
Open Source Consulting | Domain Registration