New line in php

PHP programming forum. Ask questions or help people concerning PHP code. Don't understand a function? Need help implementing a class? Don't understand a class? Here is where to ask. Remember to do your homework!

Moderator: General Moderators

Post Reply
crazycaddy
Forum Commoner
Posts: 43
Joined: Fri Aug 05, 2005 6:59 am
Location: England, UK

New line in php

Post 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?
Last edited by crazycaddy on Wed Aug 17, 2005 3:32 am, edited 1 time in total.
timvw
DevNet Master
Posts: 4897
Joined: Mon Jan 19, 2004 11:11 pm
Location: Leuven, Belgium

Post 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/>
crazycaddy
Forum Commoner
Posts: 43
Joined: Fri Aug 05, 2005 6:59 am
Location: England, UK

Post 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.
crazycaddy
Forum Commoner
Posts: 43
Joined: Fri Aug 05, 2005 6:59 am
Location: England, UK

Post 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?
User avatar
feyd
Neighborhood Spidermoddy
Posts: 31559
Joined: Mon Mar 29, 2004 3:24 pm
Location: Bothell, Washington, USA

Post by feyd »

str_replace() typically works well...



...and for future postings, don't bump a thread within 24 hours of its last post. Thanks.
crazycaddy
Forum Commoner
Posts: 43
Joined: Fri Aug 05, 2005 6:59 am
Location: England, UK

Post 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", "");
User avatar
feyd
Neighborhood Spidermoddy
Posts: 31559
Joined: Mon Mar 29, 2004 3:24 pm
Location: Bothell, Washington, USA

Post 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.
crazycaddy
Forum Commoner
Posts: 43
Joined: Fri Aug 05, 2005 6:59 am
Location: England, UK

Post by crazycaddy »

thankyou that worked perfectley.
crazycaddy
Forum Commoner
Posts: 43
Joined: Fri Aug 05, 2005 6:59 am
Location: England, UK

Post 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>";
}
?>
User avatar
feyd
Neighborhood Spidermoddy
Posts: 31559
Joined: Mon Mar 29, 2004 3:24 pm
Location: Bothell, Washington, USA

Post by feyd »

look for the "better strip_tags" link in the useful posts thread. (link in my signature)
Post Reply