Jump to content

Archived

This topic is now archived and is closed to further replies.

freeloader

Remove line break

Recommended Posts

Hi guys,

I'm writing a script that opens a file, gets the line, stores it as a variable, does the same for another file and uses them in a function. All this goes well, but when reading the line from the file, php adds a line break at the end of my variable. I tried doing streplace for \n and \r but it keeps giving the same result.

[code] $array1 = explode("\n", file_get_contents("file1.txt"));
$array2 = explode("\n", file_get_contents("file2.txt"));
$count = count($array1); 
for ($i = 0; $i < $count; $i++) 

  my_function($array1[$i],$array2[$i]); 
}

my_function($var1,$var2){
echo "variable 1: $var1 varible 2: $var2 end";
}
[/code]

The output of this code is:

variable 1: xxxx
variable 2: xxxx
end

I want it to be:

variable 1: xxxxx variable 2: xxxxx end

thanks in advance

Share this post


Link to post
Share on other sites
You can add before outputing them-

$breaks=array("\n", "\r");
$var1=str_replace($breaks, "", $var1);
$var2=str_replace($breaks, "", $var2);

Orio.

Share this post


Link to post
Share on other sites
Strange, I already tried str_replace and it didn't work before, probably made some nooby mistake. Thanks for the solution, you can close this topic :)

Share this post


Link to post
Share on other sites

×

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.