Jump to content

[SOLVED] echo multiple spaces


Minase

Recommended Posts

It really depends on what the type of your output is. If you are echoing to a plain text file, what you have will reflect the spaces in the output. On the other hand, if it is in HTML, you need to echo out the HTML entity for spaces:

<?php
echo "    Just a simple text";
?>

Link to comment
Share on other sites

str_repeat isnt usefull for this thanks anyway ;)

i do get the data from a mysql DB and echo in HTML

 

users hae a textarea and they input a description there.after that the index page will echo it,but if they put multiple spaces,in db they will exist,but at echo they disapear.

i cant use   in db,cause that value will be changed when users want,and i dont want to do a replace everytime

Link to comment
Share on other sites

<?php

$text = "     Just a simple text";
$text = str_replace(' ', '$nbsp;', $text);

echo $text;

//Output: "$nbsp;$nbsp;$nbsp;$nbsp;$nbsp;Just a simple text"

?>

 

But, you might want to consider just using PRE tags as they will also preserve line-breaks and possibly other problem characters as well.

Link to comment
Share on other sites

i cant use   in db,cause that value will be changed when users want,and i dont want to do a replace everytime

 

First off, a question: why not? When you echo HTML entities into a textarea, they are interpreted, so you should be fine with storing them as that. Secondly, though, you shouldn't do any display parsing (htmlentities and such) on INSERT into database, only when you retrieve it for display. When you insert, you should only worry about data escaping and protecting against SQL injection. Then, when you output that data, you can parse or modify it as needed for the environment in which you are working.

Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.