davefootball123 Posted April 14, 2013 Share Posted April 14, 2013 Hey all. I thought I had this issue fixed however I dont...I have a text file listed below and my preg_match statement is /TORNADO WARNING FOR:+[=NEW=\w\s\'\-]+\b('.$region.')\b/ $warnarea = array 'NIAGARA' => array('s0000076', 's0000691'), 'NIAGARA FALLS - WELLAND - SOUTHERN NIAGARA REGION' => array('s0000076'), 'ST. CATHARINES - GRIMSBY - NORTHERN NIAGARA REGION' => array('s0000691'); in the array there are 3 values which I loop through with the preg_match statement 013 WFCN11 CWTO 250127 TORNADO WARNING UPDATED BY ENVIRONMENT CANADA AT 9:27 PM EDT WEDNESDAY 24 AUGUST 2011. --------------------------------------------------------------------- TORNADO WARNING FOR: ST. CATHARINES - GRIMSBY - NORTHERN NIAGARA REGION The text file above...as you can see should only match the one array value 'ST. CATHARINES - GRIMSBY - NORTHERN NIAGARA REGION' however it matches that and NIAGARA....this is beucase in the preg_match expression I have it matching a period as well....this is the only thing causing my issue however I have no clue how to fix it. Any help would be greatly appreciated. Thanks, Dave Quote Link to comment https://forums.phpfreaks.com/topic/276925-preg_match-statement-need-help/ Share on other sites More sharing options...
davefootball123 Posted April 14, 2013 Author Share Posted April 14, 2013 (edited) EDIT: The statement is supposed to be /TORNADO WARNING FOR:+[=NEW=\w\s\'\-\.]+\b('.$region.')\b/ Sorry about that. The period in the expression needs to be there because often there are multiple locations after ST. CATHARINES... and if there period isnt matched then it wont go past that point. Dave, Edited April 14, 2013 by davefootball123 Quote Link to comment https://forums.phpfreaks.com/topic/276925-preg_match-statement-need-help/#findComment-1424641 Share on other sites More sharing options...
davefootball123 Posted April 14, 2013 Author Share Posted April 14, 2013 Sorry for the bump. I really do need some help. Thanks again, Dave Quote Link to comment https://forums.phpfreaks.com/topic/276925-preg_match-statement-need-help/#findComment-1424688 Share on other sites More sharing options...
Christian F. Posted April 15, 2013 Share Posted April 15, 2013 Could you please post all of the code that has to deal with the RegExp? I'm afraid I don't quite see what the connection between the RegeExp + the sample data and the array you posted is, and thus any attempt at helping would be just me guessing. Your expressions isn't quite correct either, as it seems you've added a random quantifier and a literal string inside a character block. This, and the little you've shown of the code, tells me that there might be some other sub-optimal choices made in the codebase. Something which we could help you iron out, if we had the whole block. Not to mention, I would really like to re-iterate my previous advice: You do not need to use regular expressions for this, as you're matching against complete strings. Thus you should not use regular expressions, seeing as they're both more complex and more wasteful than straight up string matches (via strpos). Quote Link to comment https://forums.phpfreaks.com/topic/276925-preg_match-statement-need-help/#findComment-1424783 Share on other sites More sharing options...
davefootball123 Posted April 16, 2013 Author Share Posted April 16, 2013 Thanks for the response Christian. This does require preg_match due to the variability of warning text. I have my php script shown below. What I do is I read a text file...check tornado warning and break it up into individual regions..it works great however it gets caught on the period of ST. CATHARINES in the text file and fails to go any further. I need to figure out somehow to match the period there while no going past it into the TORNADO WARNINGS ENDED FOR: section of the text file. Text file is linked below. http://www.sowx.ca/weathercentrealerts/weathercentretest.txt <?php $warnarea = array ( 'WINDSOR - LEAMINGTON - ESSEX COUNTY' => array('WINDSOR - LEAMINGTON - ESSEX COUNTY'), 'CHATHAM-KENT - RONDEAU PARK' => array('CHATHAM-KENT - RONDEAU PARK'), 'WINDSOR - ESSEX - CHATHAM-KENT' => array('WINDSOR - LEAMINGTON - ESSEX COUNTY', 'CHATHAM-KENT - RONDEAU PARK'), 'SARNIA - LAMBTON' => array('SARNIA - PETROLIA - WESTERN LAMBTON COUNTY', 'WATFORD - PINERY PARK - EASTERN LAMBTON COUNTY'), 'SARNIA - PETROLIA - WESTERN LAMBTON COUNTY' => array('SARNIA - PETROLIA - WESTERN LAMBTON COUNTY'), 'WATFORD - PINERY PARK - EASTERN LAMBTON COUNTY' => array('WATFORD - PINERY PARK - EASTERN LAMBTON COUNTY'), 'ELGIN' => array('RODNEY - SHEDDEN - WESTERN ELGIN COUNTY', 'ST. THOMAS - AYLMER - EASTERN ELGIN COUNTY'), 'RODNEY - SHEDDEN - WESTERN ELGIN COUNTY' => array('RODNEY - SHEDDEN - WESTERN ELGIN COUNTY'), 'ST. THOMAS - AYLMER - EASTERN ELGIN COUNTY' => array('ST. THOMAS - AYLMER - EASTERN ELGIN COUNTY'), 'SIMCOE - DELHI - NORFOLK' => array('SIMCOE - DELHI - NORFOLK'), 'DUNNVILLE - CALEDONIA - HALDIMAND' => array('DUNNVILLE - CALEDONIA - HALDIMAND'), 'NIAGARA' => array('NIAGARA FALLS - WELLAND - SOUTHERN NIAGARA REGION', 'ST. CATHARINES - GRIMSBY - NORTHERN NIAGARA REGION'), 'NIAGARA FALLS - WELLAND - SOUTHERN NIAGARA REGION' => array('NIAGARA FALLS - WELLAND - SOUTHERN NIAGARA REGION'), 'ST. CATHARINES - GRIMSBY - NORTHERN NIAGARA REGION' => array('ST. CATHARINES - GRIMSBY - NORTHERN NIAGARA REGION'), 'CITY OF HAMILTON' => array('CITY OF HAMILTON'), 'OXFORD - BRANT' => array('BRANTFORD - COUNTY OF BRANT', 'WOODSTOCK - TILLSONBURG - OXFORD COUNTY'), 'WOODSTOCK - TILLSONBURG - OXFORD COUNTY' => array('WOODSTOCK - TILLSONBURG - OXFORD COUNTY'), 'BRANTFORD - COUNTY OF BRANT' => array('BRANTFORD - COUNTY OF BRANT'), 'LONDON - MIDDLESEX' => array('LONDON - PARKHILL - EASTERN MIDDLESEX COUNTY', 'STRATHROY - KOMOKA - WESTERN MIDDLESEX COUNTY'), 'LONDON - PARKHILL - EASTERN MIDDLESEX COUNTY' => array('LONDON - PARKHILL - EASTERN MIDDLESEX COUNTY'), 'STRATHROY - KOMOKA - WESTERN MIDDLESEX COUNTY' => array('STRATHROY - KOMOKA - WESTERN MIDDLESEX COUNTY'), 'HURON - PERTH' => array('WINGHAM - BLYTH - NORTHERN HURON COUNTY', 'LISTOWEL - MILVERTON - NORTHERN PERTH COUNTY', 'GODERICH - BLUEWATER - SOUTHERN HURON COUNTY', 'STRATFORD - MITCHELL - SOUTHERN PERTH COUNTY'), 'WINGHAM - BLYTH - NORTHERN HURON COUNTY' => array('WINGHAM - BLYTH - NORTHERN HURON COUNTY'), 'LISTOWEL - MILVERTON - NORTHERN PERTH COUNTY' => array('LISTOWEL - MILVERTON - NORTHERN PERTH COUNTY'), 'GODERICH - BLUEWATER - SOUTHERN HURON COUNTY' => array('GODERICH - BLUEWATER - SOUTHERN HURON COUNTY'), 'STRATFORD - MITCHELL - SOUTHERN PERTH COUNTY' => array('STRATFORD - MITCHELL - SOUTHERN PERTH COUNTY'), 'WATERLOO - WELLINGTON' => array('KITCHENER - CAMBRIDGE - REGION OF WATERLOO', 'GUELPH - ERIN - SOUTHERN WELLINGTON COUNTY', 'MOUNT FOREST - ARTHUR - NORTHERN WELLINGTON COUNTY'), 'KITCHENER - CAMBRIDGE - REGION OF WATERLOO' => array('KITCHENER - CAMBRIDGE - REGION OF WATERLOO'), 'GUELPH - ERIN - SOUTHERN WELLINGTON COUNTY' => array('GUELPH - ERIN - SOUTHERN WELLINGTON COUNTY'), 'MOUNT FOREST - ARTHUR - NORTHERN WELLINGTON COUNTY' => array('MOUNT FOREST - ARTHUR - NORTHERN WELLINGTON COUNTY'), 'HALTON - PEEL' => array('CALEDON', 'MISSISSAUGA - BRAMPTON', 'HALTON HILLS - MILTON', 'BURLINGTON - OAKVILLE'), 'CALEDON' => array('CALEDON'), 'MISSISSAUGA - BRAMPTON' => array('MISSISSAUGA - BRAMPTON'), 'HALTON HILLS - MILTON' => array('HALTON HILLS - MILTON'), 'BURLINGTON - OAKVILLE' => array('BURLINGTON - OAKVILLE'), 'CITY OF TORONTO' => array('CITY OF TORONTO'), 'GREY - BRUCE' => array('BRUCE PENINSULA - SAUBLE BEACH - TOBERMORY', 'OWEN SOUND - BLUE MOUNTAINS - NORTHERN GREY COUNTY', 'SAUGEEN SHORES - KINCARDINE - SOUTHERN BRUCE COUNTY', 'HANOVER - DUNDALK - SOUTHERN GREY COUNTY'), 'BRUCE PENINSULA - SAUBLE BEACH - TOBERMORY' => array('BRUCE PENINSULA - SAUBLE BEACH - TOBERMORY'), 'OWEN SOUND - BLUE MOUNTAINS - NORTHERN GREY COUNTY' => array('OWEN SOUND - BLUE MOUNTAINS - NORTHERN GREY COUNTY'), 'SAUGEEN SHORES - KINCARDINE - SOUTHERN BRUCE COUNTY' => array('SAUGEEN SHORES - KINCARDINE - SOUTHERN BRUCE COUNTY'), 'HANOVER - DUNDALK - SOUTHERN GREY COUNTY' => array('HANOVER - DUNDALK - SOUTHERN GREY COUNTY'), 'DUFFERIN - INNISFIL' => array('INNISFIL - NEW TECUMSETH - ANGUS', 'SHELBURNE - MANSFIELD - NORTHERN DUFFERIN COUNTY', 'ORANGEVILLE - GRAND VALLEY - SOUTHERN DUFFERIN COUNTY'), 'INNISFIL - NEW TECUMSETH - ANGUS' => array('INNISFIL - NEW TECUMSETH - ANGUS'), 'SHELBURNE - MANSFIELD - NORTHERN DUFFERIN COUNTY' => array('SHELBURNE - MANSFIELD - NORTHERN DUFFERIN COUNTY'), 'ORANGEVILLE - GRAND VALLEY - SOUTHERN DUFFERIN COUNTY' => array('ORANGEVILLE - GRAND VALLEY - SOUTHERN DUFFERIN COUNTY'), 'BARRIE - ORILLIA - MIDLAND' => array('MIDLAND - COLDWATER - ORR LAKE', 'ORILLIA - LAGOON CITY - WASHAGO', 'BARRIE - COLLINGWOOD - HILLSDALE'), 'BARRIE - COLLINGWOOD - HILLSDALE' => array('BARRIE - COLLINGWOOD - HILLSDALE'), 'ORILLIA - LAGOON CITY - WASHAGO' => array('ORILLIA - LAGOON CITY - WASHAGO'), 'MIDLAND - COLDWATER - ORR LAKE' => array('MIDLAND - COLDWATER - ORR LAKE'), 'YORK - DURHAM' => array('UXBRIDGE - BEAVERTON - NORTHERN DURHAM REGION', 'NEWMARKET - GEORGINA - NORTHERN YORK REGION', 'PICKERING - OSHAWA - SOUTHERN DURHAM REGION', 'VAUGHAN - RICHMOND HILL - MARKHAM'), 'UXBRIDGE - BEAVERTON - NORTHERN DURHAM REGION' => array('UXBRIDGE - BEAVERTON - NORTHERN DURHAM REGION'), 'NEWMARKET - GEORGINA - NORTHERN YORK REGION' => array('NEWMARKET - GEORGINA - NORTHERN YORK REGION'), 'PICKERING - OSHAWA - SOUTHERN DURHAM REGION' => array('PICKERING - OSHAWA - SOUTHERN DURHAM REGION'), 'VAUGHAN - RICHMOND HILL - MARKHAM' => array('VAUGHAN - RICHMOND HILL - MARKHAM'), 'PETERBOROUGH - KAWARTHA LAKES' => array('FENELON FALLS - BALSAM LAKE PARK - NORTHERN KAWARTHA LAKES', 'APSLEY - WOODVIEW - NORTHERN PETERBOROUGH COUNTY', 'LINDSAY - SOUTHERN KAWARTHA LAKES', 'PETERBOROUGH CITY - LAKEFIELD - SOUTHERN PETERBOROUGH COUNTY'), 'FENELON FALLS - BALSAM LAKE PARK - NORTHERN KAWARTHA LAKES' => array('FENELON FALLS - BALSAM LAKE PARK - NORTHERN KAWARTHA LAKES'), 'APSLEY - WOODVIEW - NORTHERN PETERBOROUGH COUNTY' => array('APSLEY - WOODVIEW - NORTHERN PETERBOROUGH COUNTY'), 'LINDSAY - SOUTHERN KAWARTHA LAKES' => array('LINDSAY - SOUTHERN KAWARTHA LAKES'), 'PETERBOROUGH CITY - LAKEFIELD - SOUTHERN PETERBOROUGH COUNTY' => array('PETERBOROUGH CITY - LAKEFIELD - SOUTHERN PETERBOROUGH COUNTY'), 'BELLEVILLE - QUINTE - NORTHUMBERLAND' => array('BELLEVILLE - QUINTE WEST - EASTERN NORTHUMBERLAND COUNTY', 'COBOURG - COLBORNE - WESTERN NORTHUMBERLAND COUNTY'), 'BELLEVILLE - QUITE WEST - EASTERN NORTHUMBERLAND COUNTY' => array('BELLEVILLE - QUITE WEST - EASTERN NORTHUMBERLAND COUNTY'), 'COBOURG - COLBORNE - WESTERN NORTHUMBERLAND COUNTY' => array('COBOURG - COLBORNE - WESTERN NORTHUMBERLAND COUNTY') ); $data = file_get_contents('/home/sowx/public_html/weathercentrealerts/weathercentretest.txt'); if (preg_match_all('/([\w\s]+TORNADO WARNING FOR:)(.+?)\./s' , $data, $tt)) { for($i=0; $i < count($tt[0]); $i++) { $warnname[$i] = trim(rtrim($tt[1][$i])); $locales[$i] = $tt[2][$i]; $localclean = preg_replace('/\=NEW\=/' , "" , $locales[$i]); $locations = preg_split('/[\r\n]/' , $localclean); foreach($locations as $set) { $location = trim(rtrim($set)); if(array_key_exists($location, $warnarea)) { foreach($warnarea[$location] as $subregion) { echo $subregion.'<BR>'; } } } } } ?> Quote Link to comment https://forums.phpfreaks.com/topic/276925-preg_match-statement-need-help/#findComment-1424976 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.