Page 1 of 1

substring and arrays

Posted: Fri Aug 09, 2002 12:48 pm
by hob_goblin
how would i implode a string of numbers, by the lenght.. like

if i had

123456789

and i wanted it to take 3 numbers and create an array.. like

123 - 456 - 789

how would i do that?
i tried to use a for() loop, but it seems that if i did
for($i = 0; $i < $thing; $i+3){}
it wouldn't work...

any ideas?

Posted: Fri Aug 09, 2002 1:27 pm
by volka

Code: Select all

$source = "123456789";
$source=number_format($source);
$arr = explode(',', $source);
foreach($arr as $ns)
	print($ns.'<br/>');
will do it for numbers

Posted: Fri Aug 09, 2002 1:33 pm
by hob_goblin
ahh, now... let me try it, it should work, because i am working with numbers...

thanks

Posted: Fri Aug 09, 2002 1:36 pm
by hob_goblin
well, that didn't... because there is no argument to set the width of the commas... three was just an example, i really want more ( 8 )

Posted: Fri Aug 09, 2002 2:23 pm
by RandomEngy
Just wrote this script:

Code: Select all

<?php

$input = "14539592348725";
$segment_len = 4;
$result = array();
for( $i = 0; $i < strlen($input); $i += $segment_len)
  $result&#1111;] = substr($input, $i, $segment_len);

echo "<pre>";
var_dump($result);
echo "</pre>";

?>

Posted: Fri Aug 09, 2002 2:46 pm
by hob_goblin
i fixed it by doing this:

Code: Select all

for($i = 0; $i < $thing; $i++)&#123;
$x = $i * 8;
$sub = substr($foo, $x, 8);
$arr&#1111;] = $sub;
&#125;