Jump to content


Photo

Remove spaces


  • Please log in to reply
2 replies to this topic

#1 superpimp

superpimp
  • Members
  • PipPip
  • Member
  • 24 posts

Posted 12 April 2006 - 06:14 PM

<?php
$file=file('dict.txt');
$file=str_replace(' ','',$file);
foreach ($file as $file){
$file2=str_replace(' ','',$file);
echo $file2;
}
?>

The file dict.txt has 1 word on every line, no spaces after them. This bit of code echoes spaces between every word but there are no spaces in the file! I tried replacing them with nothing as you see in the code but that wont work!


What should I do?

thx!

#2 kenrbnsn

kenrbnsn
  • Staff Alumni
  • Advanced Member
  • 8,235 posts
  • LocationHillsborough, NJ, USA

Posted 12 April 2006 - 06:29 PM

The "spaces" you are seeing are really the newline characters at the end of each line. HTML will display newline characters as a space. Use the trim() function to remove the newline character before echoing the string:
<?php
$file=file('dict.txt');
foreach ($file as $file2)
   echo trim($file2);
?>

Ken

#3 superpimp

superpimp
  • Members
  • PipPip
  • Member
  • 24 posts

Posted 16 April 2006 - 10:18 AM

[!--quoteo(post=364150:date=Apr 12 2006, 08:29 PM:name=kenrbnsn)--][div class=\'quotetop\']QUOTE(kenrbnsn @ Apr 12 2006, 08:29 PM) View Post[/div][div class=\'quotemain\'][!--quotec--]
The "spaces" you are seeing are really the newline characters at the end of each line. HTML will display newline characters as a space. Use the trim() function to remove the newline character before echoing the string:
<?php
$file=file('dict.txt');
foreach ($file as $file2)
   echo trim($file2);
?>

Ken
[/quote]

thanks alot :D




0 user(s) are reading this topic

0 members, 0 guests, 0 anonymous users