Page 1 of 1

New line in php

Posted: Tue Aug 16, 2005 3:09 am
by crazycaddy
I have a script which basically writes to a file and another which turns it into an array dan displays it.

This is the script to write to the file:

Code: Select all

<?php
$name = $_POST['name'];
$des = $_POST['des'];
$content = "$name|$des\n"; 
$file = fopen("infolist.php", "r");
$read = fread($file, filesize("infolist.php"));
fclose($file);
$blah = fopen("infolist.php", "w");
$news=stripslashes($news);
fwrite($blah, "$content $read");
fclose ($blah);
print "<a href=\"../../index.php?id=4\">Back</a>";
?>
And heres the file to read it:

Code: Select all

<?php
$data = file('proe/info/infolist.php');
$data = array_reverse($data);
foreach($data as $element) {
    $element = trim($element);
    $pieces = explode("|", $element);
	    echo "<div class=\"mainDiv\" state=\"0\"><div class=\"topItem\" classOut=\"topItem\" classOver=\"topItemOver\" onMouseOver=\"Init(this);\" ><font face=\"geneva\" size=\"2px\" color=\"#000000\"> $pieces[0] - <a href=\"proe/info/delete.php?name=$pieces[0]\">Delete</a></font></div><div class=\"dropMenu\" ><div class=\"subMenu\" state=\"0\"><span class=\"subItem\" classOut=\"subItem\" classOver=\"subItemOver\"><font face=\"geneva\" size=\"2px\" color=\"#000000\"> $pieces[1] </font></span></div></div>";
}
?>
If you havent noticed already in the first piece of code, ive used \n to put the data on a new line. But this causes the file to have a blank line and the read script reconises that and displays a blank 'mainDiv'.

Is there a better way of writing the data to a new line?

Posted: Tue Aug 16, 2005 5:37 am
by timvw
You are using the text in HTML. You should know that HTML doesn't care about newlines and spaces etc..

If you want the effect of a newline, use <br/>

Posted: Tue Aug 16, 2005 5:42 am
by crazycaddy
That justs writes <br/> into the file without writing it on a new line.
using \n worked fine but because I used this the last line entered also has \n therefore creating a new line.

Posted: Tue Aug 16, 2005 7:34 am
by crazycaddy
Another Question :roll: :

Code: Select all

<?php 
$name = $_POST['name']; 
$des = $_POST['des']; 
$content = "$name|$des\n"; 
$file = fopen("infolist.php", "r"); 
$read = fread($file, filesize("infolist.php")); 
fclose($file); 
$blah = fopen("infolist.php", "w"); 
$news=stripslashes($news); 
fwrite($blah, "$content $read"); 
fclose ($blah); 
print "<a href=\"../../index.php?id=4\">Back</a>"; 
?>
$des will contain mulitple lines of data, how can I force all this into one line?

Posted: Tue Aug 16, 2005 7:37 am
by feyd
str_replace() typically works well...



...and for future postings, don't bump a thread within 24 hours of its last post. Thanks.

Posted: Tue Aug 16, 2005 7:40 am
by crazycaddy
ok, thankyou. The reason I bumped it so early is because it was almost on the second page.

I don't understand how it works, ive never been much good at understanding php.net's explanations :oops: .
I tried this:

Code: Select all

$des = $_POST['des'];
$des = str_replace("\n", "");

Posted: Tue Aug 16, 2005 7:58 am
by feyd
it doesn't matter what page the thread is on; just don't do it.

Code: Select all

$des = str_replace(array("\r\n","\n\r","\n","\r"),"\t", $des);
converts all forms of line breaks (from standard input anyways) to a tab.

Posted: Tue Aug 16, 2005 9:47 am
by crazycaddy
thankyou that worked perfectley.

Posted: Wed Aug 17, 2005 3:34 am
by crazycaddy
The main topic hasnt been solved yet :roll: (Admin changed the title, im not being impatient :wink: ).

How could I adjust this snippet so it only displays the line if it has textual content in it?

Code: Select all

<?php
$data = file('downloads/ptc/ptclist.php');
$data = array_reverse($data);
foreach($data as $element) {
    $element = trim($element);
    $pieces = explode("|", $element);
	    echo "<tr><td border=0 bordercolor=black><a href=\"$pieces[2]\">$pieces[0]</a></td><td border=0 bordercolor=black><span class=\"style2\">$pieces[1]</span></td><td border=0 bordercolor=black><a href=\"downloads/ptc/delete.php?name=$pieces[0]\">Delete</a></td></tr>";
}
?>

Posted: Wed Aug 17, 2005 7:30 am
by feyd
look for the "better strip_tags" link in the useful posts thread. (link in my signature)