Re: Re: help with end of line charater

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

 



Adam Williams wrote:
> Shawn McKenzie wrote:
>> This may be best handled in your sorting code.  What does it look like?
>>   
> 
> yeah just a second ago a big lightbulb went off in my head and i fixed
> my code to add a \r\n on saving, and strip it on viewing.  I sort on
> viewing, not sort on saving.  The viewing code looks like:
> 
> $biglist = "";
> 
> $filename = "/usr/local/majordomo/lists/".$edit;
> 
> $fp = fopen($filename, "r") or die ("Couldn't open $filename");
> if ($fp)
>        {
>        while (!feof($fp))
>                {
>                $thedata = fgets($fp);
>                $buildingvar[] = $thedata;
>                }
>                        sort($buildingvar);
>                        foreach ($buildingvar as $key => $val)
>                                {
>                                 if ($val != "\r\n")  //gets rid of empty
> whitespace lines
>                                        {
>                                        $biglist .= "$val";
>                                        }
> 
>                                }
> 
>        }
> 
> //$biglist gets echo'd into a textarea box below.
> 
> but now right before it is saved, i'll add a new line
> 
> $list .="\r\n";
> 
> $fp = fopen($filename, "w") or die ("Couldn't open $filename");
> if ($fp)
>        {
>        fwrite($fp, $list);
>        echo "
>        <head><title>Saving Page</title>
>        <link rel=stylesheet href=/maillist.css type=\"text/css\">
>        </head>
>        Your mailing list has been updated.";
>        }
> 
> fclose($fp);
> 
> so that upon viewing, if they didn't press enter, a \r\n will be added,
> and even if they did add press enter on the last line, it'll be stripped
> out upon viewing the list of email addresses.
> 
> 
> 

Taking your code, reworking it a little, this is what I came up with.

<?php

# Define & collect variables
$biglist = "";

# Input list from form in browser
$list = $_GET['list'];

# Define your source file
$filename = "/usr/local/majordomo/lists/".$edit;

# Check output/input file(s)
!is_file( $filename )		or die("File '{$filename}' does not exist.");
!is_readable( $filename )	or die("I cannot read file '{$filename}'.");
!is_writeable( $filename )	or die("I cannot write to file '{$filename}'.");

# Read file
$file_list = file( $filename );

# Clean up input from file
$file_list = array_map( 'trim', $file_list );

# Clean up input from form in browser
$list = array_map( 'trim', $list );

# Merge the two list together
$merged_list = array_merge( $list, $file_list );

# Sort stuff
sort( $merged_list );

# store the data back into the source file.
if ( file_put_contents( $filename, join(PHP_EOL, $merged_list) ) ) {
	$msg = 'Your mailing list has been updated.';
} else {
	$msg = 'Your mailing list could not be saved.';
}

# Display message to user
echo "<head><title>Saving Page</title><link rel=stylesheet href=/maillist.css type=\"text/css\"></head><body>{$msg}</body>";

?>

Hope this helps

-- 
Jim Lucas

   "Some men are born to greatness, some achieve greatness,
       and some have greatness thrust upon them."

Twelfth Night, Act II, Scene V
    by William Shakespeare

-- 
PHP General Mailing List (http://www.php.net/)
To unsubscribe, visit: http://www.php.net/unsub.php


[Index of Archives]     [PHP Home]     [Apache Users]     [PHP on Windows]     [Kernel Newbies]     [PHP Install]     [PHP Classes]     [Pear]     [Postgresql]     [Postgresql PHP]     [PHP on Windows]     [PHP Database Programming]     [PHP SOAP]

  Powered by Linux