Page 1 of 1

[SOLVED] Substr question

Posted: Tue Jul 13, 2004 10:40 am
by RedRasper
Hi All,

Quick question, has anyone every had substr do anything wierd on them? I have been using it in a loop to cut entered data into chunks. The problem is the second time around on the loop grabs far more of the string than I tell it. (I get 180 chars even though I specify 160!)

Code: Select all

<?php
$msgs = array();
$messagestart = 0;
$messageend = 160;

for($i=0;$i<$msgnum;$i++) {
	$msgs[$i] = substr($message,$messagestart,$messageend);
                $messagestart +=160;
	$messageend += 160;
}
?>
I ended up putting another line in :$msgs[$i] = substr($msgs[$i],0,160); just to get it the right length, which seems wrong to me!

If any one has any offers I appreciate it!

RedRasper

Posted: Tue Jul 13, 2004 10:45 am
by kettle_drum
string substr ( string string, int start [, int length])
$messageend += 160; is not needed.

Your code at the moment gets 160 for the first, then 320 for the second, 480 for the third etc.

Posted: Tue Jul 13, 2004 10:45 am
by feyd
the third argument is the length (number of characters) to grab.. don't modify it.

.. as kettle_drum just pointed out. ;)

Posted: Tue Jul 13, 2004 10:48 am
by RedRasper
DOH!! thank you! I knew it would have to be something stupid!!!!! I had been looking at that for ages!!!!

Cheers!