fputs - missing leading spaces

I noticed that if i try to fputs a variable that contains leading spaces, the spaces will not be sent to the file.


[php]//note the spaces in the variable before the string “data”
$data = " data to be written to the text file";

fputs($file, $data);

i haven’t found a way to resolve this yet. has anyone come across this and/or have a fix for it?



you appear to be using 4 spaces which is a tab width, try \t.

\t adds one tab space (4 spaces) but it must be enclosed within double quotes.
[php]$data = “\t data to be written to the text file”;[/php]

Let me know how you get on,
Red :wink:

that would work, except i don’t know how many blank spaces will come before the text. i am writing music chords and lyrics to a text file.


(spaces) C G Am F
the lyrics of the song go here

the first chord “C” doesn’t always go above the first word in the lyrics. for some reason fputs ignores the leading spaces before the “C”. of course i am posting these two lines into one variable. could that be the issue?

i am using this line to capture the text from a textarea named “song_data”:
[php]$song_data = stripslashes(trim($_POST[“song_data”])); [/php]

i removed the “stripslashes” and “trim” statements, but i get the same result.

this is basic, but it works. You can add as many/few spaces as you like.

[php]$song_data = ’ some blank spaces!’;
$song_data = str_replace(’ ', ’ ', $song_data);[/php]

Hope that helps,
Red :wink:

okay, i see what you’re saying. so, if the user wants to edit the song, just use the reverse command:

[php]$song_data = str_replace(’ ', ’ ', $song_data);[/php]

that should convert the “&nbsp” code back into blank spaces. then just dump the variable into the textarea like this:

[php]echo ‘’ . $song_data . ‘’;[/php]

that should work nicely. thanks a lot Redscouse!

yes that worked perfectly. i needed to remove the “trim” statement from the post, so it didn’t remove our newly-added spaces:

[php]$song_data = stripslashes($_POST[“song_data”]);
$song_data = str_replace(’ ', ’ ', $song_data);[/php]

thanks again!

You’re welcome, Happy to help.

Red :wink:

Sponsor our Newsletter | Privacy Policy | Terms of Service