Page 1 of 1

trim() - What am I doing wrong?

Posted: Wed Aug 27, 2003 5:32 am
by Derezo
the trim() function isn't removing white space from the end of strings I loaded from a file. I've also tried rtrim() but the same result.

Here's my code:

Code: Select all

function ShowProjects()
 {
  $fh = fopen("projects/list.txt","r");
  
  for($i = 0; !feof($fh); $i++)
   {
    $listї] = fgets($fh);
   }

  fclose($fh);
  
  for($j = 0; $j < $i; $j++)
   &#123;
    trim($list&#1111;$j]);
    print "<br>"".$list&#1111;$j].""<br>";
    
    $infobar = "projects/".$list&#1111;$j]."/infobar.htm";
    
    print $infobar;
    
    if(file_exists($infobar))
     &#123;
        include($infobar);
     &#125;
    else
     &#123;
      print "<br>Error Loading File";
     &#125;
   &#125;
 &#125;
Simple enough right?

The file list.txt contains 3 strings seperated by returns:
wallpaper
mud
trivia

Wallpaper and mud still have spaces after them! Trivia, however, does not.
Here's my output:
"wallpaper "
projects/wallpaper /infobar.htm
Error Loading File
"mud "
projects/mud /infobar.htm
Error Loading File
"trivia"
projects/trivia/infobar.htm

I'm very new to PHP (since Sunday), so it's probably a very simple problem. I tried a search on the forums, but found nothing..

[edit]
Also, I'm using PHP 4.3.2 and Apache 2.0.
Thanks :)

Posted: Wed Aug 27, 2003 5:46 am
by JayBird
Change this line

Code: Select all

trim($list[$j]);
to

Code: Select all

$list[$j] = trim($list[$j]);

in your code, you are trimming the line, but not storing the trimmed string anywhere.

Mark

Posted: Wed Aug 27, 2003 5:48 am
by JayBird
just had a quick look at the rest of your code. shouldn't this line

Code: Select all

$list[] = fgets($fh);
actually be

Code: Select all

$list[$i] = fgets($fh);

Posted: Wed Aug 27, 2003 6:42 am
by Derezo
Thanks - I'm use to C strings.
I knew it would be something simple. :)

The code $list[] = fgets($fh); appends it to the end of the array (right?). That's also the first reference to $list.