Jump to content

[SOLVED] In a loop read 3 characters at a time


Salis

Recommended Posts

you need to find out how many sets of three characters you have, and loop through each using substr():

 

<?php
$my_str = 'ABCDEFGHIJKLMNOPQRTSUVWXYZ';
$sets = ceil(strlength($mystring) / 3);

for ($i = 0; $i <= $sets; ++$i)
{
  $this_set = substr($my_str, $i*3, 3);
  echo 'Set number '.$i+1.': '.$this_set.'<br />';
}
?>

 

you may have to fiddle with offsets and numbers, since i haven't tested, but that should work.

Thanks guys but I'm having a bit of trouble:

akitchin, I had to change 2 minor things strlength to strlen and ++$i to $i++ to get it to work, but for some reason I only get 1. ABC and that's it. By looking at your example shouldn't it return 9 blocks?

 

lol and effigy: I have no idea if this is coincidence, probably is, but as soon as I ran the script my server became unresponsive so I'm not sure if it works...

 

EDIT:

==============================

OK Yeah, I found the error and the server is back online!

I just had to change $mystring to $my_str it's almost all ways something simple..........

Thanks a bunch guys... BTY Where would I post a math question?

Sorry, server is on line again and yeah this works great. I was able to work with both code but I'll take your advice and use effigy's example. In case you didn't see my edit, I need some (well a lot) of math help. Any one know where I should post a math question, if any where?

 

Thanks again, this really helps me a lot!

Archived

This topic is now archived and is closed to further replies.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.