Re: String Question
| From: | Martin Clifford | Date: | Wed, 31 Jul 2002 19:35:08 +0000 |
| Subject: | Re: String Question | ||
| Groups: | php.general | ||
| Request: | Send a blank email to php-general+get-110560@lists.php.net to get a copy of this message | ||
Give this a whirl:
<?php
$string = "abcdefghijklmnopqrstuvwxyz";
$len = strlen($string) // in this case, 26
for($i=0; $i < $len; $i+8) {
$str_array[] = substr($string, $i, 8);
}
for($i=0; $i < count($str_array); $i++) {
echo $str_array[$i] . "<br>\n";
}
?>
I haven't tested it, but in theory it should find the length of the string (26), loop through
it as long as $i is less than 26, incrementing 8 at a time. Each time it goes through, $str_array[]
is fed a new element, which is a substring of $string that starts at $i (0, 8, 16, 24, etc) and
grabs 8 characters. So here is how the data should be retrieved.
1st loop: 0-7 (8 elements)
2nd loop: 8-15 (8 elements)
3rd loop: 16-23 (8 elements)
4th loop: 24-26 (3 elements)
Let me know how it works out for you :o)
Martin Clifford
Homepage: http://www.completesource.net
Developer's Forums: http://www.completesource.net/forums/
>>> "Randy Johnson" <icgphp@icecoldgold.com> 07/31/02 03:26PM >>>
Hello,
I would like to be able to do the following but have not figured out a way to handle it
$string="abcdefghijklmnopqrstuvwxz";
I would like to divde the above string into separate strings 8 charactes long , example
$string1=abcdefgh
$string2=ijklmnop
$string3=qrstuvwx
$string4=yz
if the string were only 10 long
$string=abcdefghij
then
$string1=abcdefgh
$string2=ij
any suggestions would be greatly appreciated.
Thanks,
Randy