Sevvlor
Sevvlor

Reputation: 560

Whitespaces with echo (php)

I have a whitespace issue, when I echo something from the database into a textarea it adds some whitespaces.

when I use trim() it only removes the whitespaces at the beginning but there are still some left. I just can't wrap my head around it.

echo trim("[quote=".name($msg['naam'])."]\n".$msg['bericht']."[/quote]\n");

I load the quoted message with jQuery like this

function quoteMessage(pid){
    $("#post2").addClass("click");
    $(".label").addClass("hidden");
    $.post('/action-handler.php', { action: 'quote_message', pid: pid}, function(response) {
        if(response != 'ERR'){
            $('#post2').val($('#post2').val()+response);
        } else {
            $('#response').css('display', 'block');
            $("#response").append("<p class='good display-error'>An unexpected error occurred.</p>");
        }
    });
}

Upvotes: 0

Views: 2341

Answers (2)

trevorkavanaugh
trevorkavanaugh

Reputation: 340

You could regex to remove extra whitespace?

 $no_whitespace = preg_replace('/\s+/', ' ', $value);

This will turn all whitespace into a single space.

Upvotes: 0

Ivelyne Jacout
Ivelyne Jacout

Reputation: 139

For sure, your html code is like this :

<textarea>

<?php echo $value ?>

</textarea>

Then you have white space inside your textarea. You should write:

<textarea><?php echo $value ?></textarea>

See a live example here.

Upvotes: 3

Related Questions