rdrews Posted December 13, 2009 Share Posted December 13, 2009 I need a function that will search a string and return the contents of the string between two other strings. For example: $str = "1234567 this is a test and I want this text 999999"; $start = "1234567"; $end = "999999"; $textIWant = " this is a test and I want this text "; So basically I have a huge string and I want to search it for some starting string and copy all the contents between the starting string and a defined ending string into another variable. Can this be done easily? Thanks Quote Link to comment Share on other sites More sharing options...
cags Posted December 13, 2009 Share Posted December 13, 2009 Lots of ways to do it, here's one. $start = preg_quote('1234567', '#'); $end = preg_quote('999999', '#'); $str = "1234567 this is a test and I want this text 999999"; preg_match("#{$start}(.*?){$end}#s", $str, $out); print_r($out); Quote Link to comment Share on other sites More sharing options...
rdrews Posted December 13, 2009 Author Share Posted December 13, 2009 Lots of ways to do it, here's one. $start = preg_quote('1234567', '#'); $end = preg_quote('999999', '#'); $str = "1234567 this is a test and I want this text 999999"; preg_match("#{$start}(.*?){$end}#s", $str, $out); print_r($out); Cags!!! Thanks for the help again. I'm still working through the problem I was having a few days ago and I'm using both of your solutions to get through it. I think I've finally got it working now. Thanks again! Quote Link to comment 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.