473,396 Members | 1,755 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,396 software developers and data experts.

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 21665
"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 thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

1
by: Ragnorack67 | last post by:
Hi, I need some advice on what to do. I have this MySQL insert file, with about 30,000 records. One of the datafields has names in it. When this list was put together, apparently there was extra...
3
by: ReidarT | last post by:
I have accidentally enabled spaces and tabs and ... in code window. How do I disable this? reidarT
2
by: Chris Kratz | last post by:
We are having a weird problem that we ran into recently. If I use the following statements to create a test table and then run the select statement at the end, we get a very strange sort order. ...
7
by: usenet | last post by:
I would like, if it's possible, to set the value of a field in a table to a number of spaces. One space would be fine, I just want to be able to set the field to a default value that's not NULL...
135
by: Xah Lee | last post by:
Tabs versus Spaces in Source Code Xah Lee, 2006-05-13 In coding a computer program, there's often the choices of tabs or spaces for code indentation. There is a large amount of confusion about...
1
by: xagutxu | last post by:
Somebody knows how to insert one space between the charactes of a world? Thanks a lot,
8
by: Red | last post by:
If auto-format is turned off in VS2008, there is apparently no way to indent a line. Under Tools->Options->Text Editor->C#->Formatting, there are three checkboxes. Unchecking those seems to cause...
7
by: shapper | last post by:
Hello, I need to format phone numbers for easier reading: 961239070 becomes 96 123 90 70 So a space is introduced always on the places of the example. How can I do this? And can I use a...
8
by: drjay1627 | last post by:
hello, This is my 1st post here! *welcome drjay* Thanks! I look answering questions and getting answers to other! Now that we got that out of the way. I'm trying to read in a string and...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
0
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.