H4cKL0rD
H4cKL0rD

Reputation: 5508

How to fix auto newline PHP

$LINE = $User.",".$data[$k][10];
echo $LINE;
$str=implode("",file('Friends.php'));

$fp=fopen('Friends.php','w+');

$str=str_replace($User,$LINE,$str);

fwrite($fp,$str,strlen($str));

OK, this code is a bit weird but it just appends to a string of my choice. Out should come:

H4cKL0rD,9,1,2,3,4

but when it writes it it outputs into the file as

H4cKL0rD,9
,1,2,3,4

it adds a \n

Upvotes: 0

Views: 890

Answers (1)

Adam Hopkinson
Adam Hopkinson

Reputation: 28795

You need to use trim to remove whitespace and newlines from your variables:

$v = "  data\n";
$v = trim($v);
echo $v; // 'data'

I should think this is coming in from file (which reads each line into an array), so trim each line first in a loop

$str = '';
foreach(file('Friends.php') AS $line) {
$str .= trim($line);
}

Upvotes: 1

Related Questions