file fwrite() using "\r" and "\n" in str

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
User avatar
kendall
Forum Regular
Posts: 852
Joined: Tue Jul 30, 2002 10:21 am
Location: Trinidad, West Indies
Contact:

file fwrite() using "\r" and "\n" in str

Post by kendall »

Hello,

The following code is being used to write $_POST array variable to a text file.

Code: Select all

// read files function
function FILE_ACTIONS($file,$action,$string=false){
	if($action == 'r'){
		$content = file($file);
	}elseif($action == 'w' || $action == 'a'){
		$fp = fopen($file,$action);
		if(fwrite($fp,$string)){
			fclose($fp);
			return true;
		}else{
			fclose($fp);
			return false;
		}
	}
	if($content)
		return $content;
	else false;
}

$brands = $_POSTї'brand_name'];
	if(count($brands)>0){ // if array 
		$brands = join("\r",$brands);
	}else{ $brands = "ANY";}

FILE_ACTIONS($file,$action = "w",$string = $brands);
However I get the following ouput
NIKE

ADIDAS

REEBOK

FILAS

K-SWISS
What gives with the extra "new line"
User avatar
Joe
Forum Regular
Posts: 939
Joined: Sun Feb 29, 2004 1:26 pm
Location: UK - Glasgow

Post by Joe »

Try using \n instead of \r. Should work, but no to sure on that!
User avatar
kendall
Forum Regular
Posts: 852
Joined: Tue Jul 30, 2002 10:21 am
Location: Trinidad, West Indies
Contact:

Post by kendall »

Joe,

funny thing... i am trying it out using both "\r" and "\n"

in Netscape it works fine
in IE it shows the extra line....

WTF is that all about...

Kendall
User avatar
kendall
Forum Regular
Posts: 852
Joined: Tue Jul 30, 2002 10:21 am
Location: Trinidad, West Indies
Contact:

Post by kendall »

Ok,

I have been going over the code and heres what

i have substituted the \n for \r and it works

but for some reason in IE

when i

Code: Select all

<?php foreach($brands as $brand_name)&#123; ?>
          <tr> 
            <td width="30%" align="center" valign="middle" id="name"><?php echo $brand_name; ?></td>
            <td width="70%"><input name="brand_name&#1111;]" type="checkbox" id="brand_name&#1111;]" value="<?php echo $brand_name; ?>" checked></td>
          </tr>
          <?php &#125; ?>
          <tr> 
            <td>&nbsp;</td>
i get the extra lines im reading back the file using file() which reads line by line
Post Reply