[SOLVED] Substr question

PHP programming forum. Ask questions or help people concerning PHP code. Don't understand a function? Need help implementing a class? Don't understand a class? Here is where to ask. Remember to do your homework!

Moderator: General Moderators

Post Reply
RedRasper
Forum Commoner
Posts: 48
Joined: Thu Apr 24, 2003 6:36 am

[SOLVED] Substr question

Post 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
kettle_drum
DevNet Resident
Posts: 1150
Joined: Sun Jul 20, 2003 9:25 pm
Location: West Yorkshire, England

Post 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.
User avatar
feyd
Neighborhood Spidermoddy
Posts: 31559
Joined: Mon Mar 29, 2004 3:24 pm
Location: Bothell, Washington, USA

Post by feyd »

the third argument is the length (number of characters) to grab.. don't modify it.

.. as kettle_drum just pointed out. ;)
RedRasper
Forum Commoner
Posts: 48
Joined: Thu Apr 24, 2003 6:36 am

Post by RedRasper »

DOH!! thank you! I knew it would have to be something stupid!!!!! I had been looking at that for ages!!!!

Cheers!
Post Reply