Insert Spaces

Collapse
This topic is closed.
X
X
 
  • Time
  • Show
Clear All
new posts
  • Riddler

    Insert Spaces

    Anyone have an idea about how I could go about writing a function that
    would simply insert a character inbetween each character in a string?

    For instance:

    HELLO WORLD

    would turn into:

    H E L L O W O R L D

    My brain hurts from thinking about this one but I'm stumped.

    Thanks!
  • kingofkolt

    #2
    Re: Insert Spaces

    "Riddler" <riddler2224@ho tmail.com> wrote in message
    news:66odq0d79l esqe785q6injcp2 jf48jdsbs@4ax.c om...[color=blue]
    > Anyone have an idea about how I could go about writing a function that
    > would simply insert a character inbetween each character in a string?
    >
    > For instance:
    >
    > HELLO WORLD
    >
    > would turn into:
    >
    > H E L L O W O R L D
    >
    > My brain hurts from thinking about this one but I'm stumped.
    >
    > Thanks![/color]

    Tested:

    <?php
    function addspaces( $str ) {
    $temp = array();
    for ( $i = 0; $i < strlen( $str ); $i++ ) {
    $temp[$i] = $str[$i].' ';
    }
    $temp = implode( '', $temp );
    $temp = str_replace( ' ', '&nbsp;&nbsp; ', $temp );
    return $temp;
    }
    print addspaces( 'testing testing 123' );
    ?>

    HTH
    - JP


    Comment

    • Brion Vibber

      #3
      Re: Insert Spaces

      Riddler wrote:[color=blue]
      > Anyone have an idea about how I could go about writing a function that
      > would simply insert a character inbetween each character in a string?[/color]

      You can do this with a simple regular expression replacement. See the
      online manual for the Perl-compatible Regular Expression functions:
      Regular Expressions (Perl-Compatible)


      -- brion vibber (brion @ pobox.com)

      Comment

      • Pedro Graca

        #4
        Re: Insert Spaces

        Riddler wrote:[color=blue]
        > Anyone have an idea about how I could go about writing a function that
        > would simply insert a character inbetween each character in a string?[/color]

        php$ php -r 'echo preg_replace("/(.)/", "$1 ", "HELLO WORLD"), "\n";'
        H E L L O W O R L D


        Ah! -- but that might have a final (unwanted?) space. Let me check:

        php$ php -r 'echo "[", preg_replace("/(.)/", "$1 ",[color=blue]
        > "HELLO WORLD"), "]\n";'[/color]
        [H E L L O W O R L D ]


        So, I need to remove that last space:

        php$ php -r 'echo "[", substr(preg_rep lace("/(.)/", "$1 ",[color=blue]
        > "HELLO WORLD"), 0, -1), "]\n";'[/color]
        [H E L L O W O R L D]


        Ok now.

        Happy Coding :-)
        --
        Mail to my "From:" address is readable by all at http://www.dodgeit.com/
        == ** ## !! ------------------------------------------------ !! ## ** ==
        TEXT-ONLY mail to the whole "Reply-To:" address ("My Name" <my@address>)
        may bypass my spam filter. If it does, I may reply from another address!

        Comment

        • Alvaro G Vicario

          #5
          Re: Insert Spaces

          *** Riddler wrote/escribió (Fri, 26 Nov 2004 02:52:46 -0500):[color=blue]
          > Anyone have an idea about how I could go about writing a function that
          > would simply insert a character inbetween each character in a string?[/color]

          $text='Hello, world!';
          echo rtrim(preg_repl ace('/(.)/', '$1 ', $text));


          --
          -- Álvaro G. Vicario - Burgos, Spain
          -- Thank you for not e-mailing me your questions
          --

          Comment

          • Chung Leong

            #6
            Re: Insert Spaces


            "Pedro Graca" <hexkid@dodgeit .com> wrote in message
            news:slrncqe0fa .vuv.hexkid@ID-203069.user.uni-berlin.de...[color=blue]
            > Riddler wrote:[color=green]
            > > Anyone have an idea about how I could go about writing a function that
            > > would simply insert a character inbetween each character in a string?[/color]
            >
            > php$ php -r 'echo preg_replace("/(.)/", "$1 ", "HELLO WORLD"), "\n";'
            > H E L L O W O R L D
            >
            >
            > Ah! -- but that might have a final (unwanted?) space. Let me check:
            >
            > php$ php -r 'echo "[", preg_replace("/(.)/", "$1 ",[color=green]
            > > "HELLO WORLD"), "]\n";'[/color]
            > [H E L L O W O R L D ]
            >[/color]

            A lookahead assertion would solve this problem:

            echo preg_replace("/(.)(?=\S)/", "$1 ", "HELLO WORLD");

            Another way to skin this cat:

            echo trim(chunk_spli t("HELLO WORLD", 1, " "));


            Comment

            • max power

              #7
              Re: Insert Spaces

              In article <66odq0d79lesqe 785q6injcp2jf48 jdsbs@4ax.com>, Riddler
              <riddler2224@ho tmail.com> wrote:
              [color=blue]
              > Anyone have an idea about how I could go about writing a function that
              > would simply insert a character inbetween each character in a string?
              >
              > For instance:
              >
              > HELLO WORLD
              >
              > would turn into:
              >
              > H E L L O W O R L D
              >
              > My brain hurts from thinking about this one but I'm stumped.
              >
              > Thanks![/color]

              You could achieve a similar effect without having to add extra spaces
              by using the CSS property 'letter-spacing'


              e.g.
              <span style="letter-spacing: 0.5em">HELLO WORLD</span>

              or put the style declaration in an external stylesheet and assign it
              via a class selector.

              May take a bit of tweaking of the spacing value to get the exact effect
              you want. Also not sure about support for this in all browsers.

              Comment

              • John Dunlop

                #8
                Re: Insert Spaces

                max power wrote:
                [color=blue]
                > [Riddler wrote:][/color]
                [color=blue][color=green]
                > > Anyone have an idea about how I could go about writing a function that
                > > would simply insert a character inbetween each character in a string?[/color][/color]

                preg_replace('` .(?!\z)`s','$0 ',$subject)
                [color=blue]
                > You could achieve a similar effect without having to add extra spaces
                > by using the CSS property 'letter-spacing'[/color]

                Inserting a space between letters has a different effect to
                that of the letter-spacing property. Letter-spacing affects
                inter-letter spacing, a matter of style. Excepting PRE
                element content, contiguous spaces in text should collapse
                into a single one; that is, two spaces one after the other
                share the semantics of a single space.
                [color=blue]
                > Also not sure about support for this in all browsers.[/color]

                Neither am I. But browsers may treat its value as 'normal',
                regardless of its specified value.

                HAGW!

                --
                Jock

                Comment

                • max power

                  #9
                  Re: Insert Spaces

                  In article <coc8sb$smb$1@h ercules.btinter net.com>, 2metre
                  <2metre@xxxhers ham.net> wrote:
                  [color=blue][color=green]
                  > > You could achieve a similar effect without having to add extra spaces
                  > > by using the CSS property 'letter-spacing'
                  > > http://www.w3.org/TR/CSS21/text.html#spacing-props[/color]
                  >
                  > The added benefit of this method is that search engines etc will index
                  > the words correctly. (Unless of course the OP is trying to fool word
                  > recognition software!)[/color]

                  ....and also in the case where the spaced text was copied from the web
                  page to another document, it would contain the complete words without
                  the added spaces between letters.

                  Comment

                  Working...