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 Link to comment https://forums.phpfreaks.com/topic/184990-help-searching-string/ 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); Link to comment https://forums.phpfreaks.com/topic/184990-help-searching-string/#findComment-976555 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! Link to comment https://forums.phpfreaks.com/topic/184990-help-searching-string/#findComment-976556 Share on other sites More sharing options...
Recommended Posts
Archived
This topic is now archived and is closed to further replies.