Hi I just want to know how can I display more than 2 spaces in a string.

IF i have a string like

$str="(10 spaces)hello";

and If print this string it only returns the string with only one space instead of the 10 spaces 🙁

what can I do to display all the spaces before and after the string ???😕

    You could try to read up on str_repeat(on php.net):

    echo str_repeat(" ", 10);
    

    or you could choose to do this manually something like this:

    echo "with 1 space |&nbsp;|<br>";
    echo "with 4 spaces |&nbsp;&nbsp;&nbsp;&nbsp;|";
    

      that may work but I don't have a specified number of spaces, it maybe 10 maybe 14 maybe 3, etc

      I've been tried this

      str_replace(" ", "\t", $string);
      

      and

      str_replace(" ", "&nbsp", $string);
      

      but doesn't work 🙁

        didnt you need str_repeat?!

        echo str_repeat("&nbsp;", $string);
        

          I'm so confused by F-boy's question....🙂

          Do you want this?

          str_replace(" \t", "&nbsp", $string);// will change all \t to space in$string

          What do you want....

          display or filter spaces?😕

            he wants a strin with so many spaces before it. when he echos it it only display 1 space, which is stanard for HTML (i'm assuming that's what this is being used for?). if you're using CSS you could always use:

            [blockelement_where_the_text_goes] {
                white-space: pre;
            }
            

            i think, try taking out the hyphen if it doesn't work.

            hth
            moon

              nothing, but wouldn't str_pad be easier here?

              $string = str_pad($string,strlen($string)+10," ",
              STR_PAD_LEFT); //adds 10 spaces to the beginning of $string

                Wow, some hideous advice being given here.

                Your problem is not PHP but HTML. Somewhere in the HTML spec, they had the bright idea of not displaying more than one space. So you must insert the "& nbsp ;" character if you want to force the display of multiple spaces without using <pre></pre> tags.

                Without actually changing your string, I would simply do this so that spaces are reformatted to non-breaking space (& nbsp 😉 characters.

                $str="(10 spaces)hello";
                echo str_replace(" ", "&nbsp;", $str);

                str_repeat is not what you need. They misinterpreted the question.

                  The above function str_pad is able to add the spaces but then it is not displaying as it is on the browser. If the $string is going to have the spaces already for ex. " ABC" then try replacing the spaces with '&nbsp'. Something like this:

                  $string=str_replace(' ','&nbsp;',$string);
                  And try displaying the same string on the browser. It works for sure.

                  Originally posted by Moonglobe
                  nothing, but wouldn't str_pad be easier here?

                  $string = str_pad($string,strlen($string)+10," ",
                  STR_PAD_LEFT); //adds 10 spaces to the beginning of $string

                  [/B]

                    <pre>Originally posted by ybinds
                    The above function str_pad is able to add the spaces but then it is not displaying as it is on the browser. If the $string is going to have the spaces already for ex. "(10 spaces) ABC" then try replacing the spaces with '&nbsp;'. Something like this:

                    $string=str_replace(' ','&nbsp;',$string);
                    And try displaying the same string on the browser. It works for sure. </pre>

                      lol this thread is getting confusing but i get it now my first post should work:

                      Originally posted by Moonglobe
                      he wants a strin with so many spaces before it. when he echos it it only display 1 space, which is stanard for HTML (i'm assuming that's what this is being used for?). if you're using CSS you could always use:

                      [blockelement_where_the_text_goes] {
                          white-space: pre;
                      }
                      

                      i think, try taking out the hyphen if it doesn't work.

                      hth
                      moon
                      [/b]

                        I am sorry to mess it up, exactly, I was not able to display the html space that works on the browser. I guess I must first learn to post the code on this forum.

                        Originally posted by Moonglobe
                        lol this thread is getting confusing but i get it now my first post should work:

                          so do you mean that & nbsp; isn't working?😕 😕 😕

                            Yes. Exactly.

                            Originally posted by Moonglobe
                            so do you mean that & nbsp; isn't working?😕 😕 😕

                              taht's really wierd. what browser/platform are you using?

                              anyway, what's the output? have you looked at the source of the generated HTML file?

                                I am using IE and Windows XP. And moreover I was saying that after I submit this form, I am not able to see the & nbsp; as it is in my post. For ex. See the difference between the following two

                                1.$string=str_replace(' ','&nbsp;',$string);
                                2.$string=str_replace(' ','& nbsp;',$string);

                                In the first case the second parameter is getting replaced by the space. Did you get my idea now?

                                  See my answer above. It clears up the whole issue and provides the correct solution.

                                    Yep Thanx &ampnbsp;

                                    Originally posted by feldon23
                                    See my answer above. It clears up the whole issue and provides the correct solution.

                                      Write a Reply...