Salis Posted June 19, 2007 Share Posted June 19, 2007 Ok I have a question. I have a string, say it's 26 characters long. How would I read 3 characters at a time then move on to the next 3. For an example: $my_str = "ABCDEFGHIJKLMNOPQRTSUVWXYZ"; How would I read "ABC" then go to the next block "DEF" and so to the end? Quote Link to comment https://forums.phpfreaks.com/topic/56233-solved-in-a-loop-read-3-characters-at-a-time/ Share on other sites More sharing options...
akitchin Posted June 19, 2007 Share Posted June 19, 2007 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. Quote Link to comment https://forums.phpfreaks.com/topic/56233-solved-in-a-loop-read-3-characters-at-a-time/#findComment-277732 Share on other sites More sharing options...
effigy Posted June 19, 2007 Share Posted June 19, 2007 <pre> <?php $str = 'ABCDEFGHIJKLMNOPQRSTUVWXYZ'; $start = 0; while ($tmp_str = substr($str, $start, 3)) { echo $tmp_str, '<br>'; $start += 3; } ?> </pre> Quote Link to comment https://forums.phpfreaks.com/topic/56233-solved-in-a-loop-read-3-characters-at-a-time/#findComment-277734 Share on other sites More sharing options...
Salis Posted June 19, 2007 Author Share Posted June 19, 2007 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? Quote Link to comment https://forums.phpfreaks.com/topic/56233-solved-in-a-loop-read-3-characters-at-a-time/#findComment-277752 Share on other sites More sharing options...
effigy Posted June 19, 2007 Share Posted June 19, 2007 The code I posted works as is, and very quickly. Did you make any modifications? PHP version? Quote Link to comment https://forums.phpfreaks.com/topic/56233-solved-in-a-loop-read-3-characters-at-a-time/#findComment-277755 Share on other sites More sharing options...
akitchin Posted June 19, 2007 Share Posted June 19, 2007 effigy's code is far superior, don't bother even trying to modify mine. Quote Link to comment https://forums.phpfreaks.com/topic/56233-solved-in-a-loop-read-3-characters-at-a-time/#findComment-277756 Share on other sites More sharing options...
Salis Posted June 19, 2007 Author Share Posted June 19, 2007 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! Quote Link to comment https://forums.phpfreaks.com/topic/56233-solved-in-a-loop-read-3-characters-at-a-time/#findComment-277759 Share on other sites More sharing options...
effigy Posted June 19, 2007 Share Posted June 19, 2007 Try Miscellaneous. If it involves a lot of PHP, this forum should be fine. Quote Link to comment https://forums.phpfreaks.com/topic/56233-solved-in-a-loop-read-3-characters-at-a-time/#findComment-277773 Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.