Struggling with substr ();

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
User avatar
Chris Corbyn
Breakbeat Nuttzer
Posts: 13098
Joined: Wed Mar 24, 2004 7:57 am
Location: Melbourne, Australia

Struggling with substr ();

Post 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
User avatar
markl999
DevNet Resident
Posts: 1972
Joined: Thu Oct 16, 2003 5:49 pm
Location: Manchester (UK)

Post by markl999 »

echo substr($string, 4, 4);
echo substr($string, 12, 5);

It's substr($string, <start>, <length>);
Last edited by markl999 on Thu Apr 08, 2004 11:32 am, edited 1 time in total.
User avatar
kendall
Forum Regular
Posts: 852
Joined: Tue Jul 30, 2002 10:21 am
Location: Trinidad, West Indies
Contact:

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

Post 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
lostboy
Forum Contributor
Posts: 329
Joined: Mon Dec 30, 2002 8:12 pm
Location: toronto,canada

Post by lostboy »

Code: Select all

echo substr($string, 4, 7); 
echo substr($string, 11, 15);
substr starts count at 0!
User avatar
Chris Corbyn
Breakbeat Nuttzer
Posts: 13098
Joined: Wed Mar 24, 2004 7:57 am
Location: Melbourne, Australia

Post 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 :-(
Post Reply