Jump to content


Photo

Remove line break


  • Please log in to reply
2 replies to this topic

#1 freeloader

freeloader
  • Members
  • PipPipPip
  • Advanced Member
  • 175 posts

Posted 07 October 2006 - 04:17 PM

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.

$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";
}

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

#2 Orio

Orio
  • Staff Alumni
  • Advanced Member
  • 2,491 posts

Posted 07 October 2006 - 04:21 PM

You can add before outputing them-

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

Orio.
Think you're smarty?

(Gone until 20 to November)

#3 freeloader

freeloader
  • Members
  • PipPipPip
  • Advanced Member
  • 175 posts

Posted 07 October 2006 - 04:30 PM

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 :)




0 user(s) are reading this topic

0 members, 0 guests, 0 anonymous users