DaveEverFade Posted July 23, 2007 Share Posted July 23, 2007 This should be a nice easy one for you lot. I need a function that can get data from a string between 2 points. For example: If I wanted the data between BEGIN and END in the following string: 0123 BEGIN456789END 0123 BEGIN10111213END Ideally what would be returned would be an array of $data[0]=456789 $data[1]=10111213 Any ideas? Link to comment https://forums.phpfreaks.com/topic/61367-strings/ Share on other sites More sharing options...
mpharo Posted July 23, 2007 Share Posted July 23, 2007 Your question is worded kinda funny but I am assuming you want to grab the data between BEGIN and END and put it into an array? You can use the substr(); function to do this. <?php $var=BEGIN09840918END; $out=substr($var, 5, -3); echo $out; ?> This should get you the result you are looking for. Link to comment https://forums.phpfreaks.com/topic/61367-strings/#findComment-305398 Share on other sites More sharing options...
pedrobcabral Posted July 23, 2007 Share Posted July 23, 2007 This is crazy, but while you don't get any answer from a higher-skilled user you can stick with this code: <?php $string = "0123 BEGIN456789END 0123 BEGIN10111213END"; $search1 = strpos($string, "BEGIN"); $search2 = strpos($string, "END"); $s = $search2-$search1-5; $final = substr($string, ($search1+5),$s); echo $final."<br>"; $string = "0123 BEGIN456789END 0123 BEGIN10111213END"; $search1 = strrpos($string, "BEGIN"); $search2 = strrpos($string, "END"); $s = $search2-$search1-5; $final = substr($string, ($search1+5),$s); echo $final; ?> Link to comment https://forums.phpfreaks.com/topic/61367-strings/#findComment-305406 Share on other sites More sharing options...
GingerRobot Posted July 23, 2007 Share Posted July 23, 2007 Try: <?php $str = '0123 BEGIN456789END 0123 BEGIN10111213END'; preg_match_all("|BEGIN(.*?)END|",$str,$matches); print_r($matches[1]); Link to comment https://forums.phpfreaks.com/topic/61367-strings/#findComment-305417 Share on other sites More sharing options...
Recommended Posts
Archived
This topic is now archived and is closed to further replies.