drisate Posted February 26, 2009 Share Posted February 26, 2009 Hey guys i would like to find and loop a string and extract the value in between the markes into an array ... ex: 1221344136613 between 1 and 13 array[0] = 22 array[1] = 44 array[2] = 66 i created this function findinside($start, $end, $string) { preg_match_all('/' . preg_quote($start, '/') . '([^\.)]+)' . preg_quote($end, '/') . '/i', $string, $m); return $m; } But i dont think it's returning an array because i am looping the result into a foreach for then extract more values in the string. and i get preg_match_all() expects parameter 2 to be string, array given this is my full code: $content = file_get_contents("$page"); function findinside($start, $end, $string) { preg_match_all('/' . preg_quote($start, '/') . '([^\.)]+)' . preg_quote($end, '/') . '/i', $string, $m); return $m; } $start = '<table cellpadding="0" cellspacing="0" border="0" style="width:100%"><tr><td width="100%"><table cellpadding="4" cellspacing="1" border="0" style="width:100%" class="tableinborder">'; $end = "</table></td></tr></table>"; $out = findinside($start, $end, $content); foreach ($out as $loop) { //////////////// // USERNAME $txt = $loop; $re = '.*?(?:[a-z][a-z0-9_]*).*?(?:[a-z][a-z0-9_]*).*?(?:[a-z][a-z0-9_]*).*?(?:[a-z][a-z0-9_]*).*?(?:[a-z][a-z0-9_]*).*?(?:[a-z][a-z0-9_]*).*?(?:[a-z][a-z0-9_]*).*?(?:[a-z][a-z0-9_]*).*?(?:[a-z][a-z0-9_]*).*?((?:[a-z][a-z0-9_]*))';# Non-greedy match on filler if ($c = preg_match_all("/".$re."/is", $txt, $matches)) { $var1 = $matches[1][0]; print "($var1) \n"; } /////////////// // TIME $re = '((?:[0]?[1-9]|[1][012])[-:\\/.](??:[0-2]?\\d{1})|(?:[3][0,1]{1}))[-:\\/.](??:[1]{1}\\d{1}\\d{1}\\d{1})|(?:[2]{1}\\d{3})))(?![\\d]).*?((??:[0-1][0-9])|(?:[2][0-3])|(?:[0-9]))?:[0-5][0-9])(?::[0-5][0-9])?(?:\\s?(?:am|AM|pm|PM))?)';# MMDDYYYY 1 if ($c = preg_match_all("/".$re."/is", $txt, $matches)) { $mmddyyyy1 = $matches[1][0]; $time1 = $matches[2][0]; print "($mmddyyyy1) ($time1) \n"; } } I am basacly looping the content of a board thread and extract the username a time the post was posted. Quote Link to comment Share on other sites More sharing options...
Q695 Posted February 26, 2009 Share Posted February 26, 2009 That code seems to look a lot like a calender, is that what you want? Quote Link to comment Share on other sites More sharing options...
drisate Posted February 26, 2009 Author Share Posted February 26, 2009 umm no hehe i wana loop a board thread then extract for every messages of the page the username that posted the message and the time it was posted. What i need to do is breake the page in to a loop for every threads and load that to an array to then loop that array and extract the needed info. Where i block is extracting the threads between 2 values Quote Link to comment Share on other sites More sharing options...
trq Posted February 26, 2009 Share Posted February 26, 2009 How is the string 1221344136613 being created? Your going to have a hard time splitting it because I for one can see two occurences of 13 though really, whats to say there not simply occurences of 1 and 3? Quote Link to comment Share on other sites More sharing options...
drisate Posted February 26, 2009 Author Share Posted February 26, 2009 nah it's not abbout the string it self... that was just an exemple that fits in one line hehe. Like i said i am looping the HTML page of a board. it needs to loop every message posted and extract the values... $start = '<table cellpadding="0" cellspacing="0" border="0" style="width:100%"><tr><td width="100%"><table cellpadding="4" cellspacing="1" border="0" style="width:100%" class="tableinborder">'; $end = "</table></td></tr></table>"; Is how i can seperate the page. what i need to do is loop every result of whats between Quote Link to comment Share on other sites More sharing options...
Psycho Posted February 26, 2009 Share Posted February 26, 2009 I'm not that great at regular expressions, so I'm not sure if this would work in 100% of cases, but it works with the example you provided: $string = "1221344136613"; preg_match_all ( '/1{0,1}(.*?)(13)/', $string, $matches ); print_r($matches[1]); Output [0] => 22 [1] => 44 [2] => 66 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.