Groups | Search | Server Info | Keyboard shortcuts | Login | Register [http] [https] [nntp] [nntps]


Groups > comp.lang.php > #9888 > unrolled thread

how to change old ereg?

Started byastrid.kuhr@gmail.com
First post2013-06-25 21:41 -0700
Last post2013-06-28 08:48 -0400
Articles 17 — 9 participants

Back to article view | Back to comp.lang.php


Contents

  how to change old ereg? astrid.kuhr@gmail.com - 2013-06-25 21:41 -0700
    Re: how to change old ereg? Thomas 'PointedEars' Lahn <PointedEars@web.de> - 2013-06-26 11:47 +0200
    Re: how to change old ereg? Astrid <astrid.kuhr@gmail.com> - 2013-06-26 04:29 -0700
      Re: how to change old ereg? tony@mountifield.org (Tony Mountifield) - 2013-06-26 11:57 +0000
        Re: how to change old ereg? Thomas 'PointedEars' Lahn <PointedEars@web.de> - 2013-06-26 14:13 +0200
          PREG \d vs. [0-9] (was: how to change old ereg?) Christoph Michael Becker <cmbecker69@arcor.de> - 2013-06-26 16:42 +0200
            Re: PREG \d vs. [0-9] Thomas 'PointedEars' Lahn <PointedEars@web.de> - 2013-06-26 18:02 +0200
        Re: how to change old ereg? Gregor Kofler <usenet@gregorkofler.com> - 2013-06-26 21:34 +0200
          Re: how to change old ereg? Thomas 'PointedEars' Lahn <PointedEars@web.de> - 2013-06-27 01:18 +0200
            Re: how to change old ereg? The Natural Philosopher <tnp@invalid.invalid> - 2013-06-27 12:55 +0100
    Re: how to change old ereg? Astrid <astrid.kuhr@gmail.com> - 2013-06-26 05:24 -0700
    Re: how to change old ereg? Astrid <astrid.kuhr@gmail.com> - 2013-06-28 01:38 -0700
      Re: how to change old ereg? tony@mountifield.org (Tony Mountifield) - 2013-06-28 09:02 +0000
        Re: how to change old ereg? Thomas 'PointedEars' Lahn <PointedEars@web.de> - 2013-06-28 12:39 +0200
        Re: how to change old ereg? Denis McMahon <denismfmcmahon@gmail.com> - 2013-06-28 11:08 +0000
    Re: how to change old ereg? Astrid <astrid.kuhr@gmail.com> - 2013-06-28 02:46 -0700
      Re: how to change old ereg? Jerry Stuckle <jstucklex@attglobal.net> - 2013-06-28 08:48 -0400

#9888 — how to change old ereg?

Fromastrid.kuhr@gmail.com
Date2013-06-25 21:41 -0700
Subjecthow to change old ereg?
Message-ID<f60593e2-b741-40ba-9355-dcbfd41ac386@googlegroups.com>
Hello!

On a webpage I am using phpweather.
It works fine for several years.
But now it causes very many error messages.
I searched in the web, that ereg is not
longer supported and it is to replace with
preg_match.
I try to do this and change

if (ereg("^pw_${type}_([a-z][a-z])(_[A-Z][A-Z])?\.php$", $file, $regs)) {
        $output[$regs[1] . $regs[2]] = $languages[$regs[1] . $regs[2]];
      }

to

if (preg_match("/^pw_${type}_([a-z][a-z])(_[A-Z][A-Z])?\.php$/", $file, $regs)) {
        $output[$regs[1] . $regs[2]] = $languages[$regs[1] . $regs[2]];
      }

But now appears:

Notice: Undefined offset: 2 in /var/www/html/phpweather/pw_utilities.php on line 95

But I do not know php.

 } elseif (ereg_match('^M?(([0-9]?)[ ]?([0-9])(/?)([0-9]*))SM$',
                     $temp_visibility_miles . ' ' . $part, $regs)) {

I changed to

} elseif (preg_match('/^M?(([0-9]?)[ ]?([0-9])(/?)([0-9]*))SM$/',
                     $temp_visibility_miles . ' ' . $part, $regs)) {

But then:

Warning: preg_match() [function.preg-match]: Unknown modifier '?' in /var/www/html/phpweather/phpweather.php on line 329

And very very many other error-messages to.

How can I change it to get it work?

Regards, Astrid

[toc] | [next] | [standalone]


#9889

FromThomas 'PointedEars' Lahn <PointedEars@web.de>
Date2013-06-26 11:47 +0200
Message-ID<1617251.umBanJLiFt@PointedEars.de>
In reply to#9888
astrid.kuhr@gmail.com wrote:

> On a webpage I am using phpweather.
> It works fine for several years.
> But now it causes very many error messages.
> I searched in the web, that ereg is not
> longer supported and it is to replace with
> preg_match.
> I try to do this and change
> 
> if (ereg("^pw_${type}_([a-z][a-z])(_[A-Z][A-Z])?\.php$", $file, $regs)) {
>         $output[$regs[1] . $regs[2]] = $languages[$regs[1] . $regs[2]];
>       }
> 
> to
> 
> if (preg_match("/^pw_${type}_([a-z][a-z])(_[A-Z][A-Z])?\.php$/", $file,
> $regs)) {
>         $output[$regs[1] . $regs[2]] = $languages[$regs[1] . $regs[2]];
>       }
> 
> But now appears:
> 
> Notice: Undefined offset: 2 in /var/www/html/phpweather/pw_utilities.php
> on line 95
> 
> But I do not know php.

Learn it.
 
>  } elseif (ereg_match('^M?(([0-9]?)[ ]?([0-9])(/?)([0-9]*))SM$',
>                      $temp_visibility_miles . ' ' . $part, $regs)) {

There is no built-in ereg_match() function.
 
> I changed to
> 
> } elseif (preg_match('/^M?(([0-9]?)[ ]?([0-9])(/?)([0-9]*))SM$/',
>                      $temp_visibility_miles . ' ' . $part, $regs)) {
> 
> But then:
> 
> Warning: preg_match() [function.preg-match]: Unknown modifier '?' in
> /var/www/html/phpweather/phpweather.php on line 329
> 
> And very very many other error-messages to.
> 
> How can I change it to get it work?

RTFM:

<http://php.net/ereg>
<http://php.net/preg_match>


PointedEars
-- 
    realism:    HTML 4.01 Strict
    evangelism: XHTML 1.0 Strict
    madness:    XHTML 1.1 as application/xhtml+xml
                                                    -- Bjoern Hoehrmann

[toc] | [prev] | [next] | [standalone]


#9890

FromAstrid <astrid.kuhr@gmail.com>
Date2013-06-26 04:29 -0700
Message-ID<8e2894b4-16cf-4599-af33-391fc6570124@googlegroups.com>
In reply to#9888
Hello!

Sorry, it was a misstyping.

The original was:
 
  } elseif (ereg('^M?(([0-9]?)[ ]?([0-9])(/?)([0-9]*))SM$',
                      $temp_visibility_miles . ' ' . $part, $regs)) {
 
and I changed to
 
 } elseif (preg_match('/^M?(([0-9]?)[ ]?([0-9])(/?)([0-9]*))SM$/',
                     $temp_visibility_miles . ' ' . $part, $regs)) {
 

 But then:
 
 Warning: preg_match() [function.preg-match]: Unknown modifier '?' in /var/www/html/phpweather/phpweather.php on line 329
 
 And very very many other error-messages to.
 
 How can I change it to get it work?
 
 Regards, Astrid

[toc] | [prev] | [next] | [standalone]


#9891

Fromtony@mountifield.org (Tony Mountifield)
Date2013-06-26 11:57 +0000
Message-ID<kqekud$ap3$1@softins.clara.co.uk>
In reply to#9890
In article <8e2894b4-16cf-4599-af33-391fc6570124@googlegroups.com>,
Astrid  <astrid.kuhr@gmail.com> wrote:
> Hello!
> 
> Sorry, it was a misstyping.
> 
> The original was:
>  
>   } elseif (ereg('^M?(([0-9]?)[ ]?([0-9])(/?)([0-9]*))SM$',
>                       $temp_visibility_miles . ' ' . $part, $regs)) {
>  
> and I changed to
>  
>  } elseif (preg_match('/^M?(([0-9]?)[ ]?([0-9])(/?)([0-9]*))SM$/',
>                      $temp_visibility_miles . ' ' . $part, $regs)) {
>  
> 
>  But then:
>  
>  Warning: preg_match() [function.preg-match]: Unknown modifier '?' in /var/www/html/phpweather/phpweather.php on line 329
>  
>  And very very many other error-messages to.
>  
>  How can I change it to get it work?

That's because you have an unescaped / within your regex, so it sees
/^M?(([0-9]?)[ ]?([0-9])(/ followed by a ? as a regex modifier.

Try this:

} elseif (preg_match('/^M?(([0-9]?)[ ]?([0-9])(\/?)([0-9]*))SM$/',

It wasn't a problem in ereg() because the regex didn't need to be enclosed in //

Cheers
Tony
-- 
Tony Mountifield
Work: tony@softins.co.uk - http://www.softins.co.uk
Play: tony@mountifield.org - http://tony.mountifield.org

[toc] | [prev] | [next] | [standalone]


#9892

FromThomas 'PointedEars' Lahn <PointedEars@web.de>
Date2013-06-26 14:13 +0200
Message-ID<3829059.0O4M6Kg4EO@PointedEars.de>
In reply to#9891
Tony Mountifield wrote:

> Astrid  <astrid.kuhr@gmail.com> wrote:
>> The original was:
>>  
>>   } elseif (ereg('^M?(([0-9]?)[ ]?([0-9])(/?)([0-9]*))SM$',
>>                       $temp_visibility_miles . ' ' . $part, $regs)) {
>>  
>> and I changed to
>>  
>>  } elseif (preg_match('/^M?(([0-9]?)[ ]?([0-9])(/?)([0-9]*))SM$/',
>>                      $temp_visibility_miles . ' ' . $part, $regs)) {
>>  
>> 
>>  But then:
>>  
>>  Warning: preg_match() [function.preg-match]: Unknown modifier '?' in
>>  /var/www/html/phpweather/phpweather.php on line 329
>>  
>>  And very very many other error-messages to.
>>  
>>  How can I change it to get it work?
> 
> That's because you have an unescaped / within your regex, so it sees
> /^M?(([0-9]?)[ ]?([0-9])(/ followed by a ? as a regex modifier.

Good catch.  Also, in POSIX Extended Regular Expressions (ERE) this is 
written simpler

  ^M?(([0-9]?) ?([0-9])(…

and in Perl-Compatible Regular Expressions (PCRE) it is written simpler

  ^M?((\d?) ?(\d)(…

> Try this:
> 
> } elseif (preg_match('/^M?(([0-9]?)[ ]?([0-9])(\/?)([0-9]*))SM$/',
> 
> It wasn't a problem in ereg() because the regex didn't need to be enclosed
> in //

It should be noted that *any* delimiter *except* an alphanumeric character 
and backslash may be used.  This can help to make PCREs in PHP easier to 
read:

  preg_match('#^M?((\d?) ?(\d)(/?)(\d*))SM$#', …

See also: <http://php.net/preg_quote>.


PointedEars
-- 
Sometimes, what you learn is wrong. If those wrong ideas are close to the 
root of the knowledge tree you build on a particular subject, pruning the 
bad branches can sometimes cause the whole tree to collapse.
  -- Mike Duffy in cljs, <news:Xns9FB6521286DB8invalidcom@94.75.214.39>

[toc] | [prev] | [next] | [standalone]


#9894 — PREG \d vs. [0-9] (was: how to change old ereg?)

FromChristoph Michael Becker <cmbecker69@arcor.de>
Date2013-06-26 16:42 +0200
SubjectPREG \d vs. [0-9] (was: how to change old ereg?)
Message-ID<51cafdd0$0$6567$9b4e6d93@newsspool3.arcor-online.net>
In reply to#9892
Thomas 'PointedEars' Lahn wrote:
> Tony Mountifield wrote:
> 
>> That's because you have an unescaped / within your regex, so it sees
>> /^M?(([0-9]?)[ ]?([0-9])(/ followed by a ? as a regex modifier.
> 
> Good catch.  Also, in POSIX Extended Regular Expressions (ERE) this is 
> written simpler
> 
>   ^M?(([0-9]?) ?([0-9])(…
> 
> and in Perl-Compatible Regular Expressions (PCRE) it is written simpler
> 
>   ^M?((\d?) ?(\d)(…

Isn't the exact interpretation of \d locale dependent?  I was not able
to find this information on php.net and I am not able to verify this, as
I do not have locales available, which have decimal digits other than
0-9.  However, at least when one works with UTF-8 encoded strings and
uses the u modifier for the regular expression, \d is not the same as [0-9]:

  >>> $zero = "\xe0\xa5\xa6" // DEVANAGARI DIGIT ZERO
  >>> preg_match('/[0-9]/u', $zero)
  0
  >>> preg_match('/\d/u', $zero)
  1

-- 
Christoph M. Becker

[toc] | [prev] | [next] | [standalone]


#9896 — Re: PREG \d vs. [0-9]

FromThomas 'PointedEars' Lahn <PointedEars@web.de>
Date2013-06-26 18:02 +0200
SubjectRe: PREG \d vs. [0-9]
Message-ID<2167685.t7D80Y4Wy7@PointedEars.de>
In reply to#9894
Christoph Michael Becker wrote:

> Thomas 'PointedEars' Lahn wrote:
>> Tony Mountifield wrote:
>>> That's because you have an unescaped / within your regex, so it sees
>>> /^M?(([0-9]?)[ ]?([0-9])(/ followed by a ? as a regex modifier.
>> 
>> Good catch.  Also, in POSIX Extended Regular Expressions (ERE) this is
>> written simpler
>> 
>>   ^M?(([0-9]?) ?([0-9])(…
>> 
>> and in Perl-Compatible Regular Expressions (PCRE) it is written simpler
>> 
>>   ^M?((\d?) ?(\d)(…
> 
> Isn't the exact interpretation of \d locale dependent?  I was not able
> to find this information on php.net and I am not able to verify this, as
> I do not have locales available, which have decimal digits other than
> 0-9.  However, at least when one works with UTF-8 encoded strings and
> uses the u modifier for the regular expression, \d is not the same as
> [0-9]:
> 
>   >>> $zero = "\xe0\xa5\xa6" // DEVANAGARI DIGIT ZERO
>   >>> preg_match('/[0-9]/u', $zero)
>   0
>   >>> preg_match('/\d/u', $zero)
>   1

PHP uses Perl-Compatible Regular Expressions (PCRE) here:

<http://php.net/preg_match>
<http://php.net/pcre>

So this can be found in greater detail in the PCRE documentation:

<http://pcre.org/pcre.txt>

“\d” can match more than just “0” to “9” in PCRE, but (unlike in Perl [1]) 
the behavior is _not_ locale-dependent by default.  There is a flag, 
PCRE_UCP, to let \d be equivalent to \p{Digit} etc. (UCP stands for “Unicode 
Character Properties”), but apparently it is not set at compile-time for the 
default PHP distribution:

$ locale
LANG=de_CH.UTF-8
LANGUAGE=
LC_CTYPE="de_CH.UTF-8"
LC_NUMERIC="de_CH.UTF-8"
LC_TIME="de_CH.UTF-8"
LC_COLLATE="de_CH.UTF-8"
LC_MONETARY="de_CH.UTF-8"
LC_MESSAGES=en_US.UTF-8
LC_PAPER="de_CH.UTF-8"
LC_NAME="de_CH.UTF-8"
LC_ADDRESS="de_CH.UTF-8"
LC_TELEPHONE="de_CH.UTF-8"
LC_MEASUREMENT="de_CH.UTF-8"
LC_IDENTIFICATION="de_CH.UTF-8"
LC_ALL=

$ php -r 'echo setlocale(LC_ALL, "de_CH.UTF-8") . "\n";
echo preg_match("/\d/", "१");'
de_CH.UTF-8
0

The “u” expression flag in PHP sets the PCRE_UTF8 run-time flag (as 
documented), but apparently the PCRE_UCP run-time flag as well.  Hence your 
observation:

$ php -r 'echo preg_match("/\d/u", "१");'
1

$ php -v
PHP 5.4.15-1 (cli) (built: May 12 2013 12:17:45) 
Copyright (c) 1997-2013 The PHP Group
Zend Engine v2.4.0, Copyright (c) 1998-2013 Zend Technologies
    with XCache v3.0.1, Copyright (c) 2005-2013, by mOo
    with Xdebug v2.2.1, Copyright (c) 2002-2012, by Derick Rethans
    with XCache Optimizer v3.0.1, Copyright (c) 2005-2013, by mOo
    with XCache Cacher v3.0.1, Copyright (c) 2005-2013, by mOo
    with XCache Coverager v3.0.1, Copyright (c) 2005-2013, by mOo

In Perl there is the “a” flag to let Perl regular expressions match in ASCII 
mode regardless of the locale, but it is not needed with PCRE (when PCRE_UCP 
is not set at compile-time).

[1] <http://perldoc.perl.org/perlre.html>


PointedEars
-- 
Sometimes, what you learn is wrong. If those wrong ideas are close to the 
root of the knowledge tree you build on a particular subject, pruning the 
bad branches can sometimes cause the whole tree to collapse.
  -- Mike Duffy in cljs, <news:Xns9FB6521286DB8invalidcom@94.75.214.39>

[toc] | [prev] | [next] | [standalone]


#9897

FromGregor Kofler <usenet@gregorkofler.com>
Date2013-06-26 21:34 +0200
Message-ID<kqffdp$5s3$1@dont-email.me>
In reply to#9891
Am 26.06.2013 13:57, Tony Mountifield meinte:

[snip]

> That's because you have an unescaped / within your regex, so it sees
> /^M?(([0-9]?)[ ]?([0-9])(/ followed by a ? as a regex modifier.
>
> Try this:
>
> } elseif (preg_match('/^M?(([0-9]?)[ ]?([0-9])(\/?)([0-9]*))SM$/',

Or use an (in this case) "unambigious" delimiter like '~', '#', etc.

http://php.net/manual/en/regexp.reference.delimiters.php


Gregor

[toc] | [prev] | [next] | [standalone]


#9898

FromThomas 'PointedEars' Lahn <PointedEars@web.de>
Date2013-06-27 01:18 +0200
Message-ID<6604182.KQDD0VfRGQ@PointedEars.de>
In reply to#9897
Gregor Kofler wrote:

> Am 26.06.2013 13:57, Tony Mountifield meinte:
>> That's because you have an unescaped / within your regex, so it sees
>> /^M?(([0-9]?)[ ]?([0-9])(/ followed by a ? as a regex modifier.
>>
>> Try this:
>>
>> } elseif (preg_match('/^M?(([0-9]?)[ ]?([0-9])(\/?)([0-9]*))SM$/',
> 
> Or use an (in this case) "unambigious" delimiter like '~', '#', etc.

The word you were looking for is “unambiguous”.
 
> http://php.net/manual/en/regexp.reference.delimiters.php

Or use

  preg_match('/' . preg_quote('^M?((\d?) ?(\d)(/?)(\d*))SM$', '/') . '/', …

to be safe.

(There must be an error in the Matrix.)


PointedEars
-- 
Sometimes, what you learn is wrong. If those wrong ideas are close to the 
root of the knowledge tree you build on a particular subject, pruning the 
bad branches can sometimes cause the whole tree to collapse.
  -- Mike Duffy in cljs, <news:Xns9FB6521286DB8invalidcom@94.75.214.39>

[toc] | [prev] | [next] | [standalone]


#9899

FromThe Natural Philosopher <tnp@invalid.invalid>
Date2013-06-27 12:55 +0100
Message-ID<kqh97d$532$1@news.albasani.net>
In reply to#9898
On 27/06/13 00:18, Thomas 'PointedEars' Lahn wrote:
> Gregor Kofler wrote:
>
>>
>> Or use an (in this case) "unambigious" delimiter like '~', '#', etc.
> The word you were looking for is “unambiguous”.
>   

Or 'unique' :-)

-- 
Ineptocracy

(in-ep-toc’-ra-cy) – a system of government where the least capable to lead are elected by the least capable of producing, and where the members of society least likely to sustain themselves or succeed, are rewarded with goods and services paid for by the confiscated wealth of a diminishing number of producers.

[toc] | [prev] | [next] | [standalone]


#9893

FromAstrid <astrid.kuhr@gmail.com>
Date2013-06-26 05:24 -0700
Message-ID<59555d39-191a-4a15-a1b4-32d38ac5233b@googlegroups.com>
In reply to#9888
Hello Tony,

thank you very much.
Now it works very well again. :)

Regards, Astrid

[toc] | [prev] | [next] | [standalone]


#9909

FromAstrid <astrid.kuhr@gmail.com>
Date2013-06-28 01:38 -0700
Message-ID<71279fa6-1958-4b61-8758-14af767b1993@googlegroups.com>
In reply to#9888
Hello!

First all error-messages have gone now.
But now there is one coming back:

Notice: Undefined offset: 6 in /var/www/html/phpweather/phpweather.php on line 446

Notice: Undefined offset: 7 in /var/www/html/phpweather/phpweather.php on line 447

elseif (preg_match('#^(VC)?' .                           /* Proximity */
		     '(-|\+)?' .                          /* Intensity */
		     '(MI|PR|BC|DR|BL|SH|TS|FZ)?' .       /* Descriptor */
		     '((DZ|RA|SN|SG|IC|PL|GR|GS|UP)+)?' . /* Precipitation */
		     '(BR|FG|FU|VA|DU|SA|HZ|PY)?' .       /* Obscuration */
		     '(PO|SQ|FC|SS)?$#',                   /* Other */
		     $part, $regs)) {
        /*
         * Current weather-group.
         */
        $decoded_metar['weather'][] =
          array('proximity'     => $regs[1],
                'intensity'     => $regs[2],
                'descriptor'    => $regs[3],
                'precipitation' => $regs[4],
                'obscuration'   => $regs[6],    /* line 446 */
                'other'         => $regs[7]);
        
      }

I tried with several delimiters, but the error is there.
How can I get it work?

Regards, Astrid

[toc] | [prev] | [next] | [standalone]


#9911

Fromtony@mountifield.org (Tony Mountifield)
Date2013-06-28 09:02 +0000
Message-ID<kqjjfq$tsh$1@softins.clara.co.uk>
In reply to#9909
In article <71279fa6-1958-4b61-8758-14af767b1993@googlegroups.com>,
Astrid  <astrid.kuhr@gmail.com> wrote:
> Hello!
> 
> First all error-messages have gone now.
> But now there is one coming back:
> 
> Notice: Undefined offset: 6 in /var/www/html/phpweather/phpweather.php on line 446
> 
> Notice: Undefined offset: 7 in /var/www/html/phpweather/phpweather.php on line 447
> 
> elseif (preg_match('#^(VC)?' .                           /* Proximity */
> 		     '(-|\+)?' .                          /* Intensity */
> 		     '(MI|PR|BC|DR|BL|SH|TS|FZ)?' .       /* Descriptor */
> 		     '((DZ|RA|SN|SG|IC|PL|GR|GS|UP)+)?' . /* Precipitation */
> 		     '(BR|FG|FU|VA|DU|SA|HZ|PY)?' .       /* Obscuration */
> 		     '(PO|SQ|FC|SS)?$#',                   /* Other */
> 		     $part, $regs)) {
>         /*
>          * Current weather-group.
>          */
>         $decoded_metar['weather'][] =
>           array('proximity'     => $regs[1],
>                 'intensity'     => $regs[2],
>                 'descriptor'    => $regs[3],
>                 'precipitation' => $regs[4],
>                 'obscuration'   => $regs[6],    /* line 446 */
>                 'other'         => $regs[7]);
>         
>       }
> 
> I tried with several delimiters, but the error is there.
> How can I get it work?

That probably means the optional Obscuration and Other were not present.
Since nothing matched those parenthesised options, the corresponding
captures were not put in the array.

The quick and dirty solution, which I think is adequate in this instance,
is to use @ to suppress the specific errors and allow the value to be null:

          array('proximity'     => @$regs[1],
                'intensity'     => @$regs[2],
                'descriptor'    => @$regs[3],
                'precipitation' => @$regs[4],
                'obscuration'   => @$regs[6],    /* line 446 */
                'other'         => @$regs[7]);

You should only use @ to suppress errors you already understand, and as
close as possible to the specific location.

Cheers
Tony
-- 
Tony Mountifield
Work: tony@softins.co.uk - http://www.softins.co.uk
Play: tony@mountifield.org - http://tony.mountifield.org

[toc] | [prev] | [next] | [standalone]


#9914

FromThomas 'PointedEars' Lahn <PointedEars@web.de>
Date2013-06-28 12:39 +0200
Message-ID<3883513.ZhtQZqCeyo@PointedEars.de>
In reply to#9911
Tony Mountifield wrote:

> Astrid  <astrid.kuhr@gmail.com> wrote:
>> First all error-messages have gone now.
>> But now there is one coming back:
>> 
>> Notice: Undefined offset: 6 in /var/www/html/phpweather/phpweather.php on
>> line 446
>> 
>> Notice: Undefined offset: 7 in /var/www/html/phpweather/phpweather.php on
>> line 447
>> 
>> elseif (preg_match('#^(VC)?' .                           /* Proximity */
>> '(-|\+)?' .                          /* Intensity */
>> '(MI|PR|BC|DR|BL|SH|TS|FZ)?' .       /* Descriptor */
>> '((DZ|RA|SN|SG|IC|PL|GR|GS|UP)+)?' . /* Precipitation */
>> '(BR|FG|FU|VA|DU|SA|HZ|PY)?' .       /* Obscuration */
>> '(PO|SQ|FC|SS)?$#',                   /* Other */
>> $part, $regs)) {
>>         /*
>>          * Current weather-group.
>>          */
>>         $decoded_metar['weather'][] =
>>           array('proximity'     => $regs[1],
>>                 'intensity'     => $regs[2],
>>                 'descriptor'    => $regs[3],
>>                 'precipitation' => $regs[4],
>>                 'obscuration'   => $regs[6],    /* line 446 */
>>                 'other'         => $regs[7]);
>>         
>>       }
>> 
>> I tried with several delimiters, but the error is there.
>> How can I get it work?
> 
> That probably means the optional Obscuration and Other were not present.
> Since nothing matched those parenthesised options, the corresponding
> captures were not put in the array.
> 
> The quick and dirty solution, which I think is adequate in this instance,

Why?

> is to use @ to suppress the specific errors and allow the value to be
> null:
> 
>           array('proximity'     => @$regs[1],
>                 'intensity'     => @$regs[2],
>                 'descriptor'    => @$regs[3],
>                 'precipitation' => @$regs[4],
>                 'obscuration'   => @$regs[6],    /* line 446 */
>                 'other'         => @$regs[7]);
> 
> You should only use @ to suppress errors you already understand, and as
> close as possible to the specific location.

Or you could disable notices (they are _not_ error messages) entirely for 
production, and enable them only for development, as it is recommended.

However, I have also received harmless Notices in development that hindered 
development more than they helped because they broke the layout.  In that 
case I have used

  if (defined('DEBUG') && DEBUG > 0)
  {
    error_reporting(error_reporting() & ~E_NOTICE);
  }

  /* code generating harmless notices */

  if (defined('DEBUG') && DEBUG > 0)
  {
    error_reporting(error_reporting() | E_NOTICE);
  }

This approach allows you to just set the debug level to toggle development 
behavior instead of changing the (entire) code before deploying to 
production.  In my PHPX MVC framework, which is used by my ECMAScript 
Support Matrix [1], and in the Matrix itself, DEBUG === 1 enables simple 
debug messages, and DEBUG > 2 also enables database framework debug 
messages.


PointedEars
___________
[1] <http://PointedEars.de/es-matrix>
-- 
Anyone who slaps a 'this page is best viewed with Browser X' label on
a Web page appears to be yearning for the bad old days, before the Web,
when you had very little chance of reading a document written on another
computer, another word processor, or another network. -- Tim Berners-Lee

[toc] | [prev] | [next] | [standalone]


#9915

FromDenis McMahon <denismfmcmahon@gmail.com>
Date2013-06-28 11:08 +0000
Message-ID<kqjqrl$jl7$2@dont-email.me>
In reply to#9911
On Fri, 28 Jun 2013 09:02:50 +0000, Tony Mountifield wrote:

> In article <71279fa6-1958-4b61-8758-14af767b1993@googlegroups.com>,
> Astrid  <astrid.kuhr@gmail.com> wrote:
>> Hello!
>> 
>> First all error-messages have gone now.
>> But now there is one coming back:
>> 
>> Notice: Undefined offset: 6 in /var/www/html/phpweather/phpweather.php
>> on line 446
>> 
>> Notice: Undefined offset: 7 in /var/www/html/phpweather/phpweather.php
>> on line 447
>> 
>> elseif (preg_match('#^(VC)?' .                           /* Proximity
>> */
>> 		     '(-|\+)?' .                          /* Intensity */
>> 		     '(MI|PR|BC|DR|BL|SH|TS|FZ)?' .       /* Descriptor */
>> 		     '((DZ|RA|SN|SG|IC|PL|GR|GS|UP)+)?' . /* 
Precipitation */
>> 		     '(BR|FG|FU|VA|DU|SA|HZ|PY)?' .       /* Obscuration 
*/
>> 		     '(PO|SQ|FC|SS)?$#',                   /* Other */ 
$part, $regs))
>> 		     {
>>         /*
>>          * Current weather-group.
>>          */
>>         $decoded_metar['weather'][] =
>>           array('proximity'     => $regs[1],
>>                 'intensity'     => $regs[2],
>>                 'descriptor'    => $regs[3],
>>                 'precipitation' => $regs[4],
>>                 'obscuration'   => $regs[6],    /* line 446 */
>>                 'other'         => $regs[7]);
>>         
>>       }
>> 
>> I tried with several delimiters, but the error is there.
>> How can I get it work?
> 
> That probably means the optional Obscuration and Other were not present.
> Since nothing matched those parenthesised options, the corresponding
> captures were not put in the array.
> 
> The quick and dirty solution, which I think is adequate in this
> instance, is to use @ to suppress the specific errors and allow the
> value to be null:
> 
>           array('proximity'     => @$regs[1],
>                 'intensity'     => @$regs[2],
>                 'descriptor'    => @$regs[3],
>                 'precipitation' => @$regs[4],
>                 'obscuration'   => @$regs[6],    /* line 446 */
>                 'other'         => @$regs[7]);
> 
> You should only use @ to suppress errors you already understand, and as
> close as possible to the specific location.

Observation

This assumes that obscuration and other are the missing entities, and not 
eg intensity and precipitation.

A better method might be to check count($regs), and if it's less than 
expected (8)?, walk through the members from $regs[1] to $regs[last] 
assigning them to array members based on their values.

-- 
Denis McMahon, denismfmcmahon@gmail.com

[toc] | [prev] | [next] | [standalone]


#9913

FromAstrid <astrid.kuhr@gmail.com>
Date2013-06-28 02:46 -0700
Message-ID<ffde3b87-94dc-4650-9259-157207fd58cd@googlegroups.com>
In reply to#9888
Hello Tony,

yes, this did it. :)
Thank you very much.

Regards, Astrid

[toc] | [prev] | [next] | [standalone]


#9916

FromJerry Stuckle <jstucklex@attglobal.net>
Date2013-06-28 08:48 -0400
Message-ID<kqk0df$bhl$1@dont-email.me>
In reply to#9913
On 6/28/2013 5:46 AM, Astrid wrote:
> Hello Tony,
>
> yes, this did it. :)
> Thank you very much.
>
> Regards, Astrid
>

Astrid,

Please see Dennis's reply.  IMHO, Tony's "quick and dirty" solution is 
just that - and can cause numerous problems later.  It is almost never a 
good idea to suppress messages; it is better to fix them now, because if 
you don't, almost surely you'll have to fix them later.

-- 
==================
Remove the "x" from my email address
Jerry Stuckle
JDS Computer Training Corp.
jstucklex@attglobal.net
==================

[toc] | [prev] | [standalone]


Back to top | Article view | comp.lang.php


csiph-web