2

I am trying to echo an imploded PHP array into a textbox. The array is made by reading a file via the file() function, like this:

$bad_phrases=file('bad_phrases.txt', FILE_IGNORE_NEW_LINES | FILE_SKIP_EMPTY_LINES);

The array is then used in the script and I need to output it again in HTML:

<textarea>
    <?php
    echo implode("\n", $bad_phrases);
    ?>
</textarea>

However, I get some weird formatting in the textarea when I try to do this. It comes out as:

"       adsfsf
asdfsd
sddsds  "

However, When bad_phrases.txt is viewed on a browser, it shows up fine:

adsfsf
asdfsd
sddsds

And var_dump shows that the array is fine:

var_dump($bad_phrases);
array(3) { [0]=> string(6) "adsfsf" [1]=> string(6) "asdfsd" [2]=> string(6) "sddsds" }

So what am I doing wrong with the Implode?

3 Answers 3

10

Remove the space inside:

<textarea><?php echo implode("\n", $bad_phrases);?></textarea>
Sign up to request clarification or add additional context in comments.

Comments

4

You have some additional whitspace in your output, here's where it comes from:

enter image description here

Comments

0

Since the <textarea> code does not ignore whitespace, the new lines and tabs that you have are messing it up. Basically, just move the tags to something like this:

<textarea><?php
    echo implode("\n", $bad_phrases);
?></textarea>

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.