jmurch Posted April 4, 2007 Share Posted April 4, 2007 Hi All, Is there a PHP function that will trim the string and leve only the content between two delimiters? ex: whatever_the_function (delimiter, string, delimiter) so the the output is the sting with everthing before the first delimiter deleted and everything after the last delimiter deleted. so that stringstringstringstringxxxxstringstringyyyystring would result in stringstring if the first delim were xxxx and the second were yyyy. Thanks, Jeff Link to comment https://forums.phpfreaks.com/topic/45634-substringtrim-between-delimeters/ Share on other sites More sharing options...
trq Posted April 4, 2007 Share Posted April 4, 2007 Is there a PHP function that will trim the string and leve only the content between two delimiters? Nope, but you could make one using a combination of strpos and substr or maybe even ereg_replace. Link to comment https://forums.phpfreaks.com/topic/45634-substringtrim-between-delimeters/#findComment-221625 Share on other sites More sharing options...
kenrbnsn Posted April 4, 2007 Share Posted April 4, 2007 You could create your own function that uses a series of "explode()" functions to get the result: <?php function get_string($delim1,$str,$delim2) { list($dmy,$tmp) = explode($delim1,$str,2); list($tmp,$dmy) = explode($delim2,$tmp,2); return ($tmp); } $string = 'stringstringstringstringxxxxstringstringyyyystring'; $newstring = get_string('xxxx',$string,'yyyy'); echo $newstring; ?> Ken Link to comment https://forums.phpfreaks.com/topic/45634-substringtrim-between-delimeters/#findComment-221638 Share on other sites More sharing options...
jmurch Posted April 5, 2007 Author Share Posted April 5, 2007 Thanks Ken I'll give this a shot. Jeff Link to comment https://forums.phpfreaks.com/topic/45634-substringtrim-between-delimeters/#findComment-221720 Share on other sites More sharing options...
jmurch Posted April 5, 2007 Author Share Posted April 5, 2007 Ken. Thanks. Your solution is almost perfect. I should have explained it correctly in that I need to keep the delimeters as part of the final string. The output needs to be: xxxxstringstringyyyy I'm using this to strip out the XML that is outside of the body open and body closed tags so I need to use the body tags as delimeters but keep them as part of the string as well. If this is not possible I guess I could always concatonate them back in after the function has run. Thanks, Jeff Link to comment https://forums.phpfreaks.com/topic/45634-substringtrim-between-delimeters/#findComment-221775 Share on other sites More sharing options...
trq Posted April 5, 2007 Share Posted April 5, 2007 If this is not possible I guess I could always concatonate them back in after the function has run. That would be a good idea. Link to comment https://forums.phpfreaks.com/topic/45634-substringtrim-between-delimeters/#findComment-221778 Share on other sites More sharing options...
kenrbnsn Posted April 5, 2007 Share Posted April 5, 2007 Just change the function to: <?php function get_string($delim1,$str,$delim2) { list($dmy,$tmp) = explode($delim1,$str,2); list($tmp,$dmy) = explode($delim2,$tmp,2); return ($delim1 . $tmp . $delim2); } ?> Ken Link to comment https://forums.phpfreaks.com/topic/45634-substringtrim-between-delimeters/#findComment-221789 Share on other sites More sharing options...
jmurch Posted April 5, 2007 Author Share Posted April 5, 2007 DUh. I'll put on my dunce cap for asking that one........ Thanks again. Link to comment https://forums.phpfreaks.com/topic/45634-substringtrim-between-delimeters/#findComment-222579 Share on other sites More sharing options...
Recommended Posts
Archived
This topic is now archived and is closed to further replies.