469,323 Members | 1,601 Online
Bytes | Developer Community
New Post

Home Posts Topics Members FAQ

Post your question to a community of 469,323 developers. It's quick & easy.

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!
Jul 17 '05 #1
8 21155
"Riddler" <ri*********@hotmail.com> wrote in message
news:66********************************@4ax.com...
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!


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
Jul 17 '05 #2
Riddler wrote:
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?


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)
Jul 17 '05 #3
Riddler wrote:
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?
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 ", "HELLO WORLD"), "]\n";' [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 ", "HELLO WORLD"), 0, -1), "]\n";'

[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!
Jul 17 '05 #4
*** Riddler wrote/escribió (Fri, 26 Nov 2004 02:52:46 -0500):
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?


$text='Hello, world!';
echo rtrim(preg_replace('/(.)/', '$1 ', $text));
--
-- Álvaro G. Vicario - Burgos, Spain
-- Thank you for not e-mailing me your questions
--
Jul 17 '05 #5

"Pedro Graca" <he****@dodgeit.com> wrote in message
news:sl*******************@ID-203069.user.uni-berlin.de...
Riddler wrote:
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?


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 ",
"HELLO WORLD"), "]\n";'

[H E L L O W O R L D ]


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, " "));
Jul 17 '05 #6
In article <66********************************@4ax.com>, Riddler
<ri*********@hotmail.com> wrote:
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!


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.
Jul 17 '05 #7
max power wrote:
[Riddler wrote:]
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?


preg_replace('`.(?!\z)`s','$0 ',$subject)
You could achieve a similar effect without having to add extra spaces
by using the CSS property 'letter-spacing'
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.
Also not sure about support for this in all browsers.


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

HAGW!

--
Jock
Jul 17 '05 #8
In article <co**********@hercules.btinternet.com>, 2metre
<2m****@xxxhersham.net> wrote:
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


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!)


....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.
Jul 17 '05 #9

This discussion thread is closed

Replies have been disabled for this discussion.

Similar topics

1 post views Thread by Ragnorack67 | last post: by
135 posts views Thread by Xah Lee | last post: by
1 post views Thread by xagutxu | last post: by
7 posts views Thread by shapper | last post: by
reply views Thread by suresh191 | last post: by
reply views Thread by mdpf | last post: by
reply views Thread by harlem98 | last post: by
By using this site, you agree to our Privacy Policy and Terms of Use.