nadeemshafi9 Posted July 7, 2008 Share Posted July 7, 2008 hi guys i have a string, i have a position 1 and position 2, the length of the string is variable i need the text between position 1 and position 2 lets call them p1 and p2 i am looking at substring substr(text, p1, I DONT KNOW THE LENGTH) i need to work out the length from p1 to p2 in order to use substr ANY help welcome thanks Link to comment https://forums.phpfreaks.com/topic/113560-string-chops/ Share on other sites More sharing options...
br0ken Posted July 7, 2008 Share Posted July 7, 2008 <?php echo substr($var, $p1, strpos($var, $p2)); ?> Link to comment https://forums.phpfreaks.com/topic/113560-string-chops/#findComment-583471 Share on other sites More sharing options...
DarkWater Posted July 7, 2008 Share Posted July 7, 2008 @br0ken: I'm pretty sure that's not going to work. @Thread starter: I think they call it "subtraction". =P Did you try that? Link to comment https://forums.phpfreaks.com/topic/113560-string-chops/#findComment-583507 Share on other sites More sharing options...
br0ken Posted July 7, 2008 Share Posted July 7, 2008 Will this? I can't access my server so am unable to test at the minute. <?php echo substr($var, $p1, (strpos($var, $p2)-strlen($var))); ?> Link to comment https://forums.phpfreaks.com/topic/113560-string-chops/#findComment-583513 Share on other sites More sharing options...
DarkWater Posted July 7, 2008 Share Posted July 7, 2008 Uhh....I was thinking: echo substr($string, $p1, $p2-$p1); Let's say $p1 = 3 and $p2 = 7. He wants between $p1 and $p2. So he starts at $p1 (first parameter), and goes 4 forward ($p2-$p1). >_> Unless I'm just going crazy, that's how you do it. But I very well could be. Kind of just woke up. xD Link to comment https://forums.phpfreaks.com/topic/113560-string-chops/#findComment-583515 Share on other sites More sharing options...
nadeemshafi9 Posted July 7, 2008 Author Share Posted July 7, 2008 Uhh....I was thinking: echo substr($string, $p1, $p2-$p1); Let's say $p1 = 3 and $p2 = 7. He wants between $p1 and $p2. So he starts at $p1 (first parameter), and goes 4 forward ($p2-$p1). >_> Unless I'm just going crazy, that's how you do it. But I very well could be. Kind of just woke up. xD everyones sayin g thats how u do it, its not making sense to me and its the only thing holding me back how can p2-p1 be the length, eg .....p1....p2. p1 = 5 p2 = 9 p2 - p1 = 3 ??????? ... Link to comment https://forums.phpfreaks.com/topic/113560-string-chops/#findComment-583538 Share on other sites More sharing options...
DarkWater Posted July 7, 2008 Share Posted July 7, 2008 9-5 is 4, by the way. Anyway, the length parameter of the substr() function refers to how far to go from the start parameter. So if you want to go from 5-9, you start at 5 and go 4 forward (9-5). =P Get it? Link to comment https://forums.phpfreaks.com/topic/113560-string-chops/#findComment-583541 Share on other sites More sharing options...
nadeemshafi9 Posted July 7, 2008 Author Share Posted July 7, 2008 i understand now that p2 - p1 gives you the length and it is right, so thanks guys, somone in teh office already told me lol but i just couldent see it in my head even thogh i know its right, my damn algorrithm isnt working Link to comment https://forums.phpfreaks.com/topic/113560-string-chops/#findComment-583553 Share on other sites More sharing options...
Recommended Posts
Archived
This topic is now archived and is closed to further replies.