Styles2304 Posted June 3, 2008 Share Posted June 3, 2008 If I shorten this to one line, it will populate an array but otherwise, it shows me an empty array. I thought at first it was because of white space but if I remove it all and put it on one long line, it still returns an empty array. Any ideas? <?php //Setting up the Patterns $WeatherPattern = '/<td rowspan=\"3\" align=\"center\" class=\"weatherTempText\">(.*?)<\/td> <td align=\"center\" colspan=\"2\" class=\"weatherText\"> <\/td> <\/tr> <tr> <td align=\"center\" class=\"weatherText\">WIND<\/td> <td align=\"center\" class=\"weatherText\">HUMIDITY<\/td> <\/tr> <tr> <td align=\"center\" class=\"weatherText\">(.*?) (.*?)<\/td> <td align=\"center\" class=\"weatherText\">(.*?)%<\/td> <\/tr> <\/table> <\/span> <\/td> <\/tr> <tr> <td class=\"radarimage\"> <a href=\"\/content\/weather\"><img src=\"\/cache\/weather_current_conditions_radar_image\" border=0><\/a> <\/td> <\/tr> <tr> <td> <table border=\"0\" width=\"100%\" cellspacing=\"0\" cellpadding=\"0\"> <tr> <td class=\"weatherTable\" align=\"center\"> <span class=\"weatherText\">(.*?)<\/span><br> <img src=\"(.*?)\" ><br> <span class=\"weatherTemp2Text\">(.*?)<\/span> <\/td> <td class=\"weatherTable\" align=\"center\"> <span class=\"weatherText\">(.*?)<\/span><br> <img src=\"(.*?)\" ><br> <span class=\"weatherTemp2Text\">(.*?)<\/span> <\/td> <td class=\"weatherTable\" align=\"center\"> <span class=\"weatherText\">(.*?)<\/span><br> <img src=\"(.*?)\" ><br> <span class=\"weatherTemp2Text\">(.*?)<\/span>/'; //Getting the Info $Page = file_get_contents('http://mywabashvalley.com/index.php'); preg_match($WeatherPattern, $Page, $Output); print_r($Output); ?> Quote Link to comment https://forums.phpfreaks.com/topic/108548-solved-problem-with-preg_match/ Share on other sites More sharing options...
helraizer Posted June 3, 2008 Share Posted June 3, 2008 preg_match("/$WeatherPattern/i", $Page, $Output); Try that. Quote Link to comment https://forums.phpfreaks.com/topic/108548-solved-problem-with-preg_match/#findComment-556644 Share on other sites More sharing options...
hansford Posted June 3, 2008 Share Posted June 3, 2008 yeah you're going to have problems with that long a string. you should try to match the first part of the string, wildcard everything in between and match the last part of the string. Quote Link to comment https://forums.phpfreaks.com/topic/108548-solved-problem-with-preg_match/#findComment-556646 Share on other sites More sharing options...
Styles2304 Posted June 3, 2008 Author Share Posted June 3, 2008 Helraizer . . . that gave me the uknown modifier '<' error. Hansford, since I'm needing to pull parts of the website out, how do I wild card everything in between the sections I need? Quote Link to comment https://forums.phpfreaks.com/topic/108548-solved-problem-with-preg_match/#findComment-556650 Share on other sites More sharing options...
effigy Posted June 3, 2008 Share Posted June 3, 2008 <pre> <?php $page = file_get_contents('http://mywabashvalley.com/index.php'); $result = array(); $patterns = array( '/(?<=weatherTempText">)(?P<current_temp>\d+°)/', '/(?<=weatherText">)(?P<current_wind>\d+\s+[A-Z]+)/', '/(?<=weatherText">)(?P<current_humidity>\d+%)/', '/(?<=weatherText">)(?P<day>[A-Z]{3})(?![A-Z]).*?<img src="(?P<img>[^"]+).*?Temp2Text">(?P<temp>\d+°)/s' ); foreach ($patterns as $pattern) { preg_match_all($pattern, $page, $matches, PREG_SET_ORDER); $num_matches = count($matches); foreach ($matches as $match) { foreach (array_keys($match) as $key) { if (preg_match('/\A\d+\z/', $key)) { continue; } if ($num_matches == 1) { $result[$key] = $match[$key]; } else { if (!array_key_exists($key, $result) || !is_array($result[$key])) { $result[$key] = array(); } array_push($result[$key], $match[$key]); } } } } print_r($result); ?> </pre> Array ( [current_temp] => 66° [current_wind] => 13 SSE [current_humidity] => 88% [day] => Array ( [0] => TUE [1] => WED [2] => THU ) => Array ( [0] => /media/gif/PrtlyCldyTstorm0030.gif [1] => /media/gif/PrtlyCldyTstorm0030.gif [2] => /media/gif/PrtlyCldy0031.gif ) [temp] => Array ( [0] => 84° [1] => 83° [2] => 90° ) ) Quote Link to comment https://forums.phpfreaks.com/topic/108548-solved-problem-with-preg_match/#findComment-556665 Share on other sites More sharing options...
Styles2304 Posted June 3, 2008 Author Share Posted June 3, 2008 wow . . . thanks! That does the trick! Quote Link to comment https://forums.phpfreaks.com/topic/108548-solved-problem-with-preg_match/#findComment-556840 Share on other sites More sharing options...
effigy Posted June 3, 2008 Share Posted June 3, 2008 Does everything make sense? Quote Link to comment https://forums.phpfreaks.com/topic/108548-solved-problem-with-preg_match/#findComment-556846 Share on other sites More sharing options...
Styles2304 Posted June 5, 2008 Author Share Posted June 5, 2008 yup, crystal clear . . . dunno why I didn't think of that. Just a little confusing mixing the preg_match with what I've learned so far. Thanks again. Quote Link to comment https://forums.phpfreaks.com/topic/108548-solved-problem-with-preg_match/#findComment-558668 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.