0

I got a text file [list_of_files.txt] contaning list of files without extension e.g.

image_23.
image_24.
image_25.

I'm using POST method to add a single word to all of the lines e.g "jpg" and display the results:

image_23.jpg
image_24.jpg
image_25.jpg

The problem is that echo displays:

image_23. jpg
image_24. jpg
image_25. jpg

How i can remove spaces?

$files=file('list_of_files.txt');

    if ($_SERVER['REQUEST_METHOD'] == 'POST'){ 
        foreach($files as $list)
{
    $extension = $_POST['extension'];
    echo "$list",$exstension,"";
    echo "</div>";
}
 }else{ 
?>  
4
  • try trim($exstension) Commented Aug 26, 2013 at 21:44
  • note:echo "$list" is a pointless use of quotes. echo $list works just as well. Commented Aug 26, 2013 at 21:47
  • That should read as echo "$list",$extension,""; and not exstension Commented Aug 26, 2013 at 21:47
  • @Akam trim($extension) ;-) Commented Aug 26, 2013 at 21:50

5 Answers 5

4

Use trim to trim the white space.

$files=file('list_of_files.txt');

if ($_SERVER['REQUEST_METHOD'] == 'POST'){ 
    foreach($files as $list)
    {
        $extension = $_POST['extension'];
        echo trim($list) . trim($extension);
        echo "</div>";
    }
}else{ 
?>  
Sign up to request clarification or add additional context in comments.

2 Comments

Sorted! Cheers pal! :)
that's trim($extension) and not trim($exstension) ;-)
1

Try this:

echo trim($list) . trim($extension);

Comments

0
<?php
$files = file('list_of_files.txt');
if($_SERVER['REQUEST_METHOD'] == 'POST') {
    foreach($files as $list) {
        $ext = $_POST['extension'];
        $file = $list.$ext;
        echo $file;
    }
}
?>

This is a simple solution to it

Comments

0
echo $list.$extension; 

You can make a trim() on $extension before to display it to be sure there is no space.

Comments

0

use trim function

echo trim($list) . trim($extension);

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.