Page 1 of 1

Struggling with substr ();

Posted: Thu Apr 08, 2004 11:25 am
by Chris Corbyn
Hi,

I'm trying to use substr(); to pick bits of this string:

Code: Select all

$string = 'px0v[b]8400[/b]bns[b]19320[/b]k6ow';
I need to bits in bold. Am I right to use:

Code: Select all

echo substr($string, 4, ;
echo substr($string, 12, 16);
Because it's not returning the correct parts of the string????

I'm probably just being an idiot but I can't figure out how to use this substr command. I've used it before to just know off the last 4 characters in a string but never to pick out bits in the middle.

Please help. I feel stupid asking but I can'tget it to work for some reason?

Thanks

Posted: Thu Apr 08, 2004 11:29 am
by markl999
echo substr($string, 4, 4);
echo substr($string, 12, 5);

It's substr($string, <start>, <length>);

Posted: Thu Apr 08, 2004 11:30 am
by kendall
if your

Code: Select all

substr($string,4,8);
and your starting from offest of 4 then you need to count from 4 meaning 4 = 0 thus...

Code: Select all

substr($string,4,4);
Kendall

Posted: Thu Apr 08, 2004 11:30 am
by kettle_drum
Your almost there but the third parameter to substr is the number of chars it should read - not the number from the start to read to. So it should be:

Code: Select all

echo substr($string, 4, 4); //start at char 4 and read 4
echo substr($string, 12, 5); //start at char 12 and read 5

Posted: Thu Apr 08, 2004 11:30 am
by lostboy

Code: Select all

echo substr($string, 4, 7); 
echo substr($string, 11, 15);
substr starts count at 0!

Posted: Thu Apr 08, 2004 11:31 am
by Chris Corbyn
Aaah, thanks I didn't realise it was start point then legth. i thought it was start the end. That explains it then. Thanks very much :-)

I'm so stupid :-(