combo box returning a space at the end?

PHP programming forum. Ask questions or help people concerning PHP code. Don't understand a function? Need help implementing a class? Don't understand a class? Here is where to ask. Remember to do your homework!

Moderator: General Moderators

Post Reply
slipstream
Forum Commoner
Posts: 86
Joined: Fri Apr 19, 2002 8:53 am
Location: Canada

combo box returning a space at the end?

Post by slipstream »

I have a combo box where the user selects files to delete. then I add a ".txt" to it so the user can delete a txt file of their choice.

Anyway this is what it returns:

myfile .txt

when there shouldn't be a space after the name and the .txt. Any ideas how to remove it?
Galahad
Forum Contributor
Posts: 111
Joined: Fri Jun 14, 2002 5:50 pm

Post by Galahad »

Can you show us how the box is being created? If all else fails, you can do a trim or rtrim on the string before you append ".txt" to it.
slipstream
Forum Commoner
Posts: 86
Joined: Fri Apr 19, 2002 8:53 am
Location: Canada

Post by slipstream »

the box is being created like this:

echo '<option value="-1">Please Select...</option>';
foreach($file as $line)
{
$info = explode('|', $line);
echo '<option value="'.$info[0].'">'.$info[0].'</option>';

}
echo '</select><BR><BR>';
Galahad
Forum Contributor
Posts: 111
Joined: Fri Jun 14, 2002 5:50 pm

Post by Galahad »

I suspect your $line is formatted something like "text you want | stuff you don't want". If that's right, when you call

Code: Select all

<?php
explode("|", $line);
?>
it will leave the space in. Again, assuming my guess about the format is right, you could do

Code: Select all

<?php
explode(" | ", $line);  // Note the spaces around the '|' character
?>
to get rid of the spaces, too.

If it isn't always " | ", you could explode on just "|" like you are and do something like:

Code: Select all

<?php
 echo '<option value="'.trim($info[0]).'">'.trim($info[0]).'</option>';
?>
when you make the box.
Post Reply