Jump to content

Recommended Posts

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.

 

Link to comment
https://forums.phpfreaks.com/topic/277091-help-with-peculiar-preg_match-problem/
Share on other sites

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.

 

 

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.

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.