Connecting Tech Pros Worldwide Forums | Help | Site Map

Insert Spaces

Riddler
Guest
 
Posts: n/a
#1: Jul 17 '05
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
Guest
 
Posts: n/a
#2: Jul 17 '05

re: Insert Spaces


"Riddler" <riddler2224@hotmail.com> wrote in message
news:66odq0d79lesqe785q6injcp2jf48jdsbs@4ax.com...[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


Brion Vibber
Guest
 
Posts: n/a
#3: Jul 17 '05

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:
http://www.php.net/pcre

-- brion vibber (brion @ pobox.com)
Pedro Graca
Guest
 
Posts: n/a
#4: Jul 17 '05

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_replace("/(.)/", "$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!
Alvaro G Vicario
Guest
 
Posts: n/a
#5: Jul 17 '05

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_replace('/(.)/', '$1 ', $text));


--
-- Álvaro G. Vicario - Burgos, Spain
-- Thank you for not e-mailing me your questions
--
Chung Leong
Guest
 
Posts: n/a
#6: Jul 17 '05

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_split("HELLO WORLD", 1, " "));


max power
Guest
 
Posts: n/a
#7: Jul 17 '05

re: Insert Spaces


In article <66odq0d79lesqe785q6injcp2jf48jdsbs@4ax.com>, Riddler
<riddler2224@hotmail.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'
http://www.w3.org/TR/CSS21/text.html#spacing-props

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.
John Dunlop
Guest
 
Posts: n/a
#8: Jul 17 '05

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
max power
Guest
 
Posts: n/a
#9: Jul 17 '05

re: Insert Spaces


In article <coc8sb$smb$1@hercules.btinternet.com>, 2metre
<2metre@xxxhersham.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.
Closed Thread