ProgrammingThis forum is for all programming questions.
The question does not have to be directly related to Linux and any language is fair game.
Notices
Welcome to LinuxQuestions.org, a friendly and active Linux Community.
You are currently viewing LQ as a guest. By joining our community you will have the ability to post topics, receive our newsletter, use the advanced search, subscribe to threads and access many other special features. Registration is quick, simple and absolutely free. Join our community today!
Note that registered members see fewer ads, and ContentLink is completely disabled once you log in.
If you have any problems with the registration process or your account login, please contact us. If you need to reset your password, click here.
Having a problem logging in? Please visit this page to clear all LQ-related cookies.
Get a virtual cloud desktop with the Linux distro that you want in less than five minutes with Shells! With over 10 pre-installed distros to choose from, the worry-free installation life is here! Whether you are a digital nomad or just looking for flexibility, Shells can put your Linux machine on the device that you want to use.
Exclusive for LQ members, get up to 45% off per month. Click here for more info.
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.
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.
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.
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.
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 */ $chrlen= strlen($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>
<input name="name" type="text" size="25"><br>
Homepage,<BR>
<input name="homepage" type="text" size="25"><br>
E-mail address,<BR>
<input name="email" type="text" size="25"><br>
<BR>
Comments for the guest book,<br>
<textarea name="comments" rows="7" cols="47"></textarea>
<BR>
<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 */ $chrlen= strlen($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
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?
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($comments, 50); $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>
<input name="name" type="text" size="25"><br>
Homepage,<BR>
<input name="homepage" type="text" size="25"><br>
E-mail address,<BR>
<input name="email" type="text" size="25"><br>
<BR>
Comments for the guest book,<br>
<textarea name="comments" rows="7" cols="47"></textarea>
<BR>
<input type="reset" value="Erase and start over">
<input type="submit" value="Click here to submit">
</form>
</body>
</html>
LinuxQuestions.org is looking for people interested in writing
Editorials, Articles, Reviews, and more. If you'd like to contribute
content, let us know.