dondmite Posted April 18, 2013 Share Posted April 18, 2013 I am trying to convert some PHP code, changing all ereg to preg_match but ran into a problem where one of my statements is refusing to cooperate. The statement in question is shown on the first line in the code snippet below: . . if (ereg('^(M?[0-9]{2})/(M?[0-9]{2}|[X]{2})?$',$part,$pieces)) { $tempC = (integer) strtr($pieces[1], 'M', '-'); $tempF = round(1.8 * $tempC + 32); get_wind_chill($tempF, $wxInfo); // Other dependent functions . } The new change to the ereg line I am trying to use is: . if (preg_match('/^(M?[0-9]{2})/(M?[0-9]{2}|[X]{2})?$/',$part,$pieces)) { .The code looks for a string like 30/25 or 08/07 or M01/M11 or 07/M01 - temperatures and dewpoints respectively as reported in METARs - (Meteorological Reports), where M represents the "-" for negative values.The ereg version works perfectly fine and has been doing so for several years. Now that I'm updating the code, adding some new functions and removing deprecated features like ereg, the very last ereg line needing to be changed is mis-behaving. Maybe I am tired and not seeing what else needs to be changed. Could you please tell me what is wrong here why my preg_match version in this remaining line is refusing to work.Thanks in advance. Quote Link to comment https://forums.phpfreaks.com/topic/277091-help-with-peculiar-preg_match-problem/ Share on other sites More sharing options...
requinix Posted April 18, 2013 Share Posted April 18, 2013 Only thing you haven't said is how it's not working. Not matching on a particular string? What string? Getting the wrong numbers? Something else entirely? Quote Link to comment https://forums.phpfreaks.com/topic/277091-help-with-peculiar-preg_match-problem/#findComment-1425524 Share on other sites More sharing options...
dondmite Posted April 18, 2013 Author Share Posted April 18, 2013 I know it was definitely tiredness that got the better of me. I took a couple of hours rest then came back to the problem. All of a sudden it was staring me right in the face. I forgot to escape the slash. My code should have been. if (preg_match('/^(M?[0-9]{2})\/(M?[0-9]{2}|[X]{2})?$/',$part,$pieces)) { I left out the back slash shown in red above. By the way, the code was not doing any matching thus causing all of the dependent functions to stop working. Quote Link to comment https://forums.phpfreaks.com/topic/277091-help-with-peculiar-preg_match-problem/#findComment-1425532 Share on other sites More sharing options...
requinix Posted April 18, 2013 Share Posted April 18, 2013 Heh. I was going to include my usual "how to convert ereg/POSIX expressions to preg_match/PCRE expressions" but figured you didn't need that. What's funny is that the spiel would have said something about adding delimiters and then escaping that character in the expression if it's used. Quote Link to comment https://forums.phpfreaks.com/topic/277091-help-with-peculiar-preg_match-problem/#findComment-1425538 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.