-1

I want to use the project PHP Weather 2.2.2 but it has many problems here is a code

function get_languages($type) {

    static $output = array();

    if (empty($output)) {

        /* I use dirname(__FILE__) here instead of PHPWEATHER_BASE_DIR
         * because one might use this function without having included
         * phpweather.php first. */
        require(dirname(__FILE__) . '/languages.php');

        $dir = opendir(dirname(__FILE__) . '/output');
        while($file = readdir($dir)) {

            // I got a problem in  this line below     

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

            // After i change it to the line mentioned below i got error undefined offset:2 in line 2 mentioned below
            // how to correct this error or is there any way to correct this code

            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]];
            }
        }
        closedir($dir);
    }

    asort($output);

    return $output;
}
Barmar
  • 741,623
  • 53
  • 500
  • 612

2 Answers2

0

You see this notice because the $regs[2] is not set.

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

use isset() for checking is exists an array value or no. If no exists concatenation with empty string ''

That is because you use ? in your regex (_[A-Z][A-Z])?

Victor Bocharsky
  • 11,930
  • 13
  • 58
  • 91
0

Use this regular expression:

        if(preg_match("#^pw_${type}_([a-z][a-z])((?:_[A-Z][A-Z])?)\.php$#", $file, $regs)) {

The group with ?: will not be a capture group, it exists just to allow you to apply the ? modifier to it. Wrapping it in another group captures the contents of that; if the optional regexp wasn't matched, this will be an empty string.

Barmar
  • 741,623
  • 53
  • 500
  • 612