kmoney12
kmoney12

Reputation: 4490

Imploding PHP Array Into HTML Textarea

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?

Upvotes: 2

Views: 5496

Answers (3)

Vreality
Vreality

Reputation: 305

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>

Upvotes: 0

AndreKR
AndreKR

Reputation: 33678

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

enter image description here

Upvotes: 4

Ibu
Ibu

Reputation: 43820

Remove the space inside:

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

Upvotes: 10

Related Questions