script needed

polm

Diamond Member
May 24, 2001
3,183
0
0
I need to instert a single space after every fourth character in a string of characters.

thanks,

lazy
 

shuan24

Platinum Member
Jul 17, 2003
2,558
0
0
1. for (i = 0; i < length of string+spaces; i+=4)
insert space;
2. ....

3. profit
 

Kyteland

Diamond Member
Dec 30, 2002
5,747
1
81
Here is some pseudocode. Go nuts.

int i=0;
while (!EOF)
{
i++;
putchar(getchar());
if (i%4 == 0)
putchar(" ");
}
 

Crusty

Lifer
Sep 30, 2001
12,684
2
81
Originally posted by: polm
Originally posted by: MCrusty
WRONG FORUM

who cares

Obviously not you! :p

If you posted in Software I would more then gladly help you, but since you don't seem to care about that, I could care less about helping :)
 

Beau

Lifer
Jun 25, 2001
17,730
0
76
www.beauscott.com
<?php
$str = 'asdfasdfasdfasdfasdfasdfasdfasdfasdf'; // your subject string
$delimiter = '^'; // Character(s) to be inserted after every 4.

$broken = preg_split('/\r\n/', chunk_split($str, 4));
$result = join($broken, $delimiter);
?>
 

polm

Diamond Member
May 24, 2001
3,183
0
0
thanks all, even you MCrusty.

I'll be sure to post in Software next time ;)
 

Beau

Lifer
Jun 25, 2001
17,730
0
76
www.beauscott.com
Originally posted by: notfred
s/(.{4})/$1 /g;

hehe, tried that at first with preg_split... but I was an idiot and didn't realize that it would remove whatever it matched on. Forgot about preg_replace.

:)

<?php
$result = preg_replace('/(.{4})/', '$1' . $string_delimiter, $string_subject);
?>