Rifts Posted January 6, 2012 Share Posted January 6, 2012 can someone please help me I need to get all the info between these two things _MARKER_OBJ = [ (all this stuff) ] thanks Quote Link to comment https://forums.phpfreaks.com/topic/254505-i-hate-preg_match/ Share on other sites More sharing options...
Rifts Posted January 6, 2012 Author Share Posted January 6, 2012 why doesnt this work function getTextBetweenTags($string, $start, $end) { $pattern = "/$start(.*?)$end/"; preg_match($pattern, $string, $matches); return $matches[1]; } $html = 'this is a bunch of nothing _MARKER_OBJ = [ get this crap ]'; $content = getTextBetweenTags($html, '_MARKER_OBJ = [', ']'); echo $content; it just returns nothing, if I change the start to just _MARKER_OBJ it works though Quote Link to comment https://forums.phpfreaks.com/topic/254505-i-hate-preg_match/#findComment-1304984 Share on other sites More sharing options...
Rifts Posted January 6, 2012 Author Share Posted January 6, 2012 figured it out need to add \ before the [ so $content = getTextBetweenTags($html, '_MARKER_OBJ = \[', ']'); works Quote Link to comment https://forums.phpfreaks.com/topic/254505-i-hate-preg_match/#findComment-1304988 Share on other sites More sharing options...
ragax Posted January 6, 2012 Share Posted January 6, 2012 Hi Rifts, That's a neat little function that would solve about half of the requests for regex help on the www!!! If I was building a library of extended preg functions, I would include that. Quote Link to comment https://forums.phpfreaks.com/topic/254505-i-hate-preg_match/#findComment-1304997 Share on other sites More sharing options...
JAY6390 Posted January 6, 2012 Share Posted January 6, 2012 If you're wanting to restrict it to just text either side as the "tags" you could use function getTextBetweenTags($string, $start, $end) { $start = preg_quote($start, '~'); $end = preg_quote($end, '~'); $pattern = "~$start(.*?)$end~"; if(preg_match($pattern, $string, $matches)) { return $matches[1]; } return false; } $html = 'this is a bunch of nothing _MARKER_OBJ = [ get this crap ]'; $content = getTextBetweenTags($html, '_MARKER_OBJ = [', ']'); echo $content; That doesn't require you to escape the start/end tags as it escapes all regex syntax characters using preg_quote Quote Link to comment https://forums.phpfreaks.com/topic/254505-i-hate-preg_match/#findComment-1305021 Share on other sites More sharing options...
Rifts Posted January 7, 2012 Author Share Posted January 7, 2012 ohhhh cool I'm really new to regex but that is awesome to know! Quote Link to comment https://forums.phpfreaks.com/topic/254505-i-hate-preg_match/#findComment-1305120 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.