Click to See Complete Forum and Search --> : Adding <br>


GavinPearce
11-16-2003, 08:36 AM
I have a a script that write a form input to .sng file (which
using htaccess that I have setup on the server is excatly the
same as a php file) Also when putting a word like don't in it adds a slash in front of the apostrophe.

So hence if you save the data as:

Line 1 text you're here
Line 2 text here

When the browser reads it it doesn't see a <br> command and so prints the above as:

Line 1 text you\'re hereLine 2 text here
(note this forum must be using the technology I need as it won't display the slash i purposely put in front of the ' above and it inserts line breaks in the correct place.

My two question are: How do you get the script to insert <br> tags where they are needed, and make sure it doesn't save the slashs at the same time.

If someone could put it in the script I have below I would be most greatful!

Cheers all!


<?
include ("config.php");

echo ($Style);

if($song == "" || $lyrics == "")
{
$HTML = ($sHeaderHTML);
$HTML .= ("<form method=\"post\" action=\"add.php\">");
$HTML .= ($sBeforeHTML . "Song name:" . $sAfterHTML . $sBeforeHTML . "<input name=\"song\"><br>" . $sAfterHTML . $sSplitHTML);
$HTML .= ($sBeforeHTML . "Midi URL:" . $sAfterHTML . $sBeforeHTML . "<input name=\"midiurl\"><br>" . $sAfterHTML . $sSplitHTML);
$HTML .= ($sBeforeHTML . "Lyrics:" . $sAfterHTML . $sBeforeHTML . "<textarea name=\"lyrics\" rows=\"10\" cols=\"40\"></textarea><br>" . $sAfterHTML . $sSplitHTML);
$HTML .= ($sBeforeHTML . "<input type=\"submit\" value=\"Submit\">" . $sAfterHTML);
$HTML .= ($sBeforeHTML . "<input type=\"reset\" value=\"Reset\">" . $sAfterHTML);
$HTML .= ($sFooterHTML);
print ($HTML);
}
else
{
if($autoadd == (false))
{
$filename = ("l-".$song.".dat");
}
else
{
$filename = ("l-".$song.".sng");
}
if(file_exists($filename))
{
echo ("The lyrics you submitted already exist");
}
else
{
$data .= $HNewTemp;
$data .= "Song name: ".$song."<br>\n";
$data .= "Midi URL: <a href='midi/".$midiurl."' onclick=\"NewWindow(this.href,'lmidi','200','100','no','center');return false\">Open Midi File</a><br>\n";
$data .= "Lyrics: ".$lyrics."\n";
$data .= $FNewTemp;
$openfile = fopen(("$filename"),"w");
fwrite($openfile, "$data");
fclose($openfile);
echo ("<script language=\"JavaScript\">window.location='add.php'</script>");
}
}
?>

pyro
11-16-2003, 09:08 AM
nl2br() (http://us3.php.net/manual/en/function.nl2br.php)
stripslashes() (http://us3.php.net/manual/en/function.stripslashes.php)

GavinPearce
11-16-2003, 09:17 AM
Ok so I have


<?
function nl2br($str) {
$str = preg_replace('/\n/i', '<br />', $str);
}

function br2nl($str) {
$str = preg_replace('/<br( )?(\/)?>/i', '\n', $str);
}

// And a fun little xhtml function to use
function br2xhtml($str) {
$str = preg_replace('/<br( )?(\/)?>/i', '<br />', $str);
}
?>


and


echo stripslashes($str);


I don't suppose you could insert the two functions into my orginal script for me please pyro?

GavinPearce
11-16-2003, 09:19 AM
Also found..


<?
if (get_magic_quotes_gpc()."" == "1") {
foreach($_GET as $k=>$v) $_GET["$k"]=stripslashes($v);
foreach($_POST as $k=>$v) $_POST["$k"]=stripslashes($v);
foreach($_COOKIE as $k=>$v) $_COOKIE["$k"]=stripslashes($v);
}
// Strips all extra quotes
?>


...if it's any use

Cheers again

pyro
11-16-2003, 09:28 AM
Change:

$openfile = fopen(("$filename"),"w");
fwrite($openfile, "$data");
fclose($openfile);To:

$contents = file($filename);
$lines = "";
foreach ($contents as $line) {
$lines .= $line;
}
echo nl2br(stripslashes($lines));

GavinPearce
11-16-2003, 10:02 AM
Doesn't work. Now it's not writing a file at all.


<?
include ("config.php");

echo ($Style);

if($song == "" || $lyrics == "")
{
$HTML = ($sHeaderHTML);
$HTML .= ("<form method=\"post\" action=\"add.php\">");
$HTML .= ($sBeforeHTML . "Song name:" . $sAfterHTML . $sBeforeHTML . "<input name=\"song\"><br>" . $sAfterHTML . $sSplitHTML);
$HTML .= ($sBeforeHTML . "Midi URL:" . $sAfterHTML . $sBeforeHTML . "<input name=\"midiurl\"><br>" . $sAfterHTML . $sSplitHTML);
$HTML .= ($sBeforeHTML . "Lyrics:" . $sAfterHTML . $sBeforeHTML . "<textarea name=\"lyrics\" rows=\"10\" cols=\"40\"></textarea><br>" . $sAfterHTML . $sSplitHTML);
$HTML .= ($sBeforeHTML . "<input type=\"submit\" value=\"Submit\">" . $sAfterHTML);
$HTML .= ($sBeforeHTML . "<input type=\"reset\" value=\"Reset\">" . $sAfterHTML);
$HTML .= ($sFooterHTML);
print ($HTML);
}
else
{
if($autoadd == (false))
{
$filename = ("l-".$song.".dat");
}
else
{
$filename = ("l-".$song.".sng");
}
if(file_exists($filename))
{
echo ("The lyrics you submitted already exist");
}
else
{
$data .= $HNewTemp;
$data .= "Song name: ".$song."<br>\n";
$data .= "Midi URL: <a href='midi/".$midiurl."' onclick=\"NewWindow(this.href,'lmidi','200','100','no','center');return false\">Open Midi File</a><br>\n";
$data .= "Lyrics: ".$lyrics."\n";
$data .= $FNewTemp;
$contents = file($filename);
$lines = "";
foreach ($contents as $line) {
$lines .= $line;
}
echo nl2br(stripslashes($lines));
echo ("<script language=\"JavaScript\">window.location='add.php'</script>");
}
}
?>

pyro
11-16-2003, 10:10 AM
Cripes, my bad. In your original script, what prints out the files contents?

GavinPearce
11-16-2003, 10:28 AM
$filename = ("l-".$song.".sng"); //sets the filename
}
if(file_exists($filename)) //if it is already there
{
echo ("The lyrics you submitted already exist");
}
else
{

// All data to be writen into the file

$data .= $HNewTemp;
$data .= "Song name: ".$song."<br>\n";
$data .= "Midi URL: <a href='midi/".$midiurl."' onclick=\"NewWindow(this.href,'lmidi','200','100','no','center');return false\">Open Midi File</a><br>\n";
$data .= "Lyrics: ".$lyrics."\n";
$data .= $FNewTemp;
$openfile = fopen(("$filename"),"w"); //open file
fwrite($openfile, "$data"); // write to file
fclose($openfile); //close file thats open


The data is input by the top half of the main script (above is jsut hte bottom half) which is a form that will keep apearing unless the two required fields are filled in. When filled in it sets the filename and writes the contents of the form into certain sections. I.e there is a form input called lyrics, and it add that as data to the file using: $data .= "Lyrics: ".$lyrics."\n";

GavinPearce
11-16-2003, 11:15 AM
The script basically takes a form input and saves it with a few extra values stored in a config file to the same directory with an extension of .sng which using .htaccess I have set-up to be the same as a .php extension on the server.

pyro
11-16-2003, 12:39 PM
Unless I'm missing it, you still haven't shown what code you are using to display the contents of the file.

GavinPearce
11-16-2003, 01:06 PM
I'm not. I need the strip slashes bit done when it writes the file rather than when you call it back.

The file is just being called in the browser like a normal .htm file.

pyro
11-16-2003, 01:22 PM
$data = stripslashes($data);
fwrite ($openfile, $data);

GavinPearce
11-16-2003, 01:25 PM
how about the line break bit?

pyro
11-16-2003, 01:30 PM
See if you can get it from this line, which I gave you earlier:
Originally posted by pyro
echo nl2br(stripslashes($lines));

GavinPearce
11-16-2003, 01:32 PM
mayb dis

$data = nl2br(stripslashes($data));
fwrite ($openfile, $data);


?

pyro
11-16-2003, 01:35 PM
It's lookin' good to me...

GavinPearce
11-16-2003, 02:08 PM
It works! Cheers.


Only problem now is it puts it for all the data and I only want it to do it for part. I might be able to figure out how, but if you can see a way...

pyro
11-16-2003, 02:12 PM
Depends what data you want to do it to... ;)

GavinPearce
11-16-2003, 02:16 PM
Dis is the final script. I've added a comment where I want it.


<?
include ("config.php");

echo ($Style);

if($song == "" || $lyrics == "")
{
$HTML = ($sHeaderHTML);
$HTML .= ("<form method=\"post\" action=\"add.php\">");
$HTML .= ($sBeforeHTML . "Song name:" . $sAfterHTML . $sBeforeHTML . "<input name=\"song\"><br>" . $sAfterHTML . $sSplitHTML);
$HTML .= ($sBeforeHTML . "Midi URL:" . $sAfterHTML . $sBeforeHTML . "<input name=\"midiurl\"><br>" . $sAfterHTML . $sSplitHTML);
$HTML .= ($sBeforeHTML . "Lyrics:" . $sAfterHTML . $sBeforeHTML . "<textarea name=\"lyrics\" rows=\"10\" cols=\"40\"></textarea><br>" . $sAfterHTML . $sSplitHTML);
$HTML .= ($sBeforeHTML . "<input type=\"submit\" value=\"Submit\">" . $sAfterHTML);
$HTML .= ($sBeforeHTML . "<input type=\"reset\" value=\"Reset\">" . $sAfterHTML);
$HTML .= ($sFooterHTML);
print ($HTML);
}
else
{
if($autoadd == (false))
{
$filename = ("l-".$song.".dat");
}
else
{
$filename = ("l-".$song.".sng");
}
if(file_exists($filename))
{
echo ("The lyrics you submitted already exist");
}
else
{
$data .= $HNewTemp; //not on this one
$data .= "Song name: ".$song."<br>\n"; //not on this one
$data .= "Midi URL: <a href='midi/".$midiurl."' onclick=\"NewWindow(this.href,'lmidi','200','100','no','center');return false\">Open Midi File</a><br>\n"; //not on this one
$data .= "Lyrics: ".$lyrics."\n"; // I only want it on this one
$data .= $FNewTemp; //not on this one
$data = nl2br(stripslashes("$data")); // currently does all
$openfile = fopen(("$filename"),"w");
fwrite ($openfile, $data);
fclose($openfile);
echo ("<script language=\"JavaScript\">window.location='add.php'</script>");
}
}
?>

pyro
11-16-2003, 04:03 PM
Try this:$data .= $HNewTemp; //not on this one
$data .= "Song name: ".$song."<br>\n"; //not on this one
$data .= "Midi URL: <a href='midi/".$midiurl."' onclick=\"NewWindow(this.href,'lmidi','200','100','no','center');return false\">Open Midi File</a><br>\n"; //not on this one
$data .= nl2br(stripslashes("Lyrics: ".$lyrics."\n")); // I only want it on this one
$data .= $FNewTemp; //not on this one

GavinPearce
11-17-2003, 01:22 AM
Yer I just tried that before I came here and it works a treat!

Cheers Pyro for all your help!

pyro
11-17-2003, 07:27 AM
You're welcome... :)