refiking Posted July 21, 2009 Share Posted July 21, 2009 I need to delete everything before $section1 and everything after $section2. So, //IF $string = "12345" $section1 = 12; $section2 = 4; //$newstring should return 35 Quote Link to comment https://forums.phpfreaks.com/topic/166804-solved-parse-string-from-the-middle-of-a-string/ Share on other sites More sharing options...
Alex Posted July 21, 2009 Share Posted July 21, 2009 You mean delete everything before or after (like you said) Or delete everything within those 2 variables, like you demonstrated? $string = "12345" $section1 = 12; $section2 = 4; $newstring = str_replace(Array($section1, $section2), NULL, $string); echo $newstring; // 35 Quote Link to comment https://forums.phpfreaks.com/topic/166804-solved-parse-string-from-the-middle-of-a-string/#findComment-879578 Share on other sites More sharing options...
refiking Posted July 21, 2009 Author Share Posted July 21, 2009 Sorry. Value should be 3 not 35. Quote Link to comment https://forums.phpfreaks.com/topic/166804-solved-parse-string-from-the-middle-of-a-string/#findComment-879581 Share on other sites More sharing options...
rhodesa Posted July 21, 2009 Share Posted July 21, 2009 <?php $string = "012333456"; $section1 = '12'; $section2 = '4'; $start = strpos($string,$section1)+strlen($section1); $end = strpos($string,$section2,$start); $newstring = substr($string,$start,$end-$start); echo $newstring; // 3 Quote Link to comment https://forums.phpfreaks.com/topic/166804-solved-parse-string-from-the-middle-of-a-string/#findComment-879588 Share on other sites More sharing options...
refiking Posted July 21, 2009 Author Share Posted July 21, 2009 Nevermind... Forgot to change the $string variable Quote Link to comment https://forums.phpfreaks.com/topic/166804-solved-parse-string-from-the-middle-of-a-string/#findComment-879591 Share on other sites More sharing options...
rhodesa Posted July 21, 2009 Share Posted July 21, 2009 that means $section1 wasn't found in the string. did you make sure and use quotes around your $string, $section1, and $section2 values? Quote Link to comment https://forums.phpfreaks.com/topic/166804-solved-parse-string-from-the-middle-of-a-string/#findComment-879596 Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.