为什么从 PHP 5 升级到 PHP 7 时出现 "Closing delimiter C not found" 错误

Why am I getting "Closing delimiter C not found" error when upgrading from PHP 5 to PHP 7

我正在使用 php7cc 工具 https://github.com/sstalle/php7cc 来帮助升级到 PHP7。它返回一个我不知道如何修复的错误,因为它没有给出行号,而且我不完全知道它要我修复什么。我得到一个

"text" : "Closing delimiter C not found",
              "line" : null

错误来自从 php7cc 返回的 json。这是返回它的页面:

<?php
class geo
{
var $postcode = "";
var $lat = 0;
var $lng = 0;

function setPostcode($postcode)
{
    $this->postcode = preg_replace("/[^a-zA-Z0-9]/", "", strtoupper(trim($postcode)));
}

function getPostcode()
{
    return $this->postcode;
}

function getLatLong($postcode="")
{
    if($postcode)
        $this->setPostcode($postcode);

    if($this->isValidPostCode())
    {
        if($_SESSION['GEO'][$this->postcode])
            return $_SESSION['GEO'][$this->postcode];

        $url = "http://maps.google.co.uk/maps/api/geocode/json?address=".$this->postcode."&sensor=false&region=uk";
        $jsonData = file_get_contents($url);

        if($jsonData)
        {
            $data = json_decode($jsonData);

            $this->lng = $data->results[0]->geometry->location->lng;
            $this->lat = $data->results[0]->geometry->location->lat;

            if($this->lng && $this->lat)
                $result = array("postcode"=>$this->postcode, "lat"=>$this->lat, "lng"=>$this->lng);
            else
                $result = array("error"=>"Postcode doesn't exist");

            $_SESSION['GEO'][$this->postcode] = $result;
            return $_SESSION['GEO'][$this->postcode];
        }
        return array("error", "Unable to fetch lat/long");
    }
    else
    {
        return array("error"=>"Invalid postcode");
    }
}

function isValidPostCode()
{
    // Permitted letters depend upon their position in the postcode.
    $alpha1 = "[abcdefghijklmnoprstuwyz]";                          // Character 1
    $alpha2 = "[abcdefghklmnopqrstuvwxy]";                          // Character 2
    $alpha3 = "[abcdefghjkstuw]";                                   // Character 3
    $alpha4 = "[abehmnprvwxy]";                                     // Character 4
    $alpha5 = "[abdefghjlnpqrstuwxyz]";                             // Character 5

    // Expression for postcodes: AN NAA, ANN NAA, AAN NAA, and AANN NAA
    $pcexp[0] = '^('.$alpha1.'{1}'.$alpha2.'{0,1}[0-9]{1,2})([0-9]{1}'.$alpha5.'{2})$';

    // Expression for postcodes: ANA NAA
    $pcexp[1] =  '^('.$alpha1.'{1}[0-9]{1}'.$alpha3.'{1})([0-9]{1}'.$alpha5.'{2})$';

    // Expression for postcodes: AANA NAA
    $pcexp[2] =  '^('.$alpha1.'{1}'.$alpha2.'[0-9]{1}'.$alpha4.')([0-9]{1}'.$alpha5.'{2})$';

    // Exception for the special postcode GIR 0AA
    $pcexp[3] =  '^(gir)(0aa)$';

    // Standard BFPO numbers
    $pcexp[4] = '^(bfpo)([0-9]{1,4})$';

    // c/o BFPO numbers
    $pcexp[5] = '^(bfpo)(c\/o[0-9]{1,3})$';

    // Load up the string to check, converting into lowercase and removing spaces
    $postcode = strtolower($this->postcode);

    // Assume we are not going to find a valid postcode
    $valid = false;

    // Check the string against the six types of postcodes
    foreach ($pcexp as $regexp)
    {
        if (preg_match($regexp,$postcode, $matches))
        {

          // Load new postcode back into the form element
          $toCheck = strtoupper ($matches[1] . ' ' . $matches [2]);

          // Take account of the special BFPO c/o format
          $toCheck = preg_replace ('C/O', 'c/o ', $toCheck);

          // Remember that we have found that the code is valid and break from loop
          $valid = true;
          break;
        }
    }

    // Return with the reformatted valid postcode in uppercase if the postcode was
    // valid
    if ($valid){return true;} else {return false;};
}
}
?>

这段代码不是我写的。我的猜测与 preg_replace ('C/O', 'c/o ', $toCheck); 有关,但我真的不知道。

我已尝试简化您的示例以对其进行测试。

我发现的唯一明显的事情是您的正则表达式字符串没有分隔符,例如

$pattern="/^([0-9]+)$/";

参见:PHP-Delimiters

此外,在 preg_replace 中还有一个未转义的 / 如果您添加标准分隔符。这可能是导致错误的原因。

<?php

isValidPostCode();

function isValidPostCode()
{
    // Permitted letters depend upon their position in the postcode.
    $alpha1 = "[abcdefghijklmnoprstuwyz]";                          // Character 1
    $alpha2 = "[abcdefghklmnopqrstuvwxy]";                          // Character 2
    $alpha3 = "[abcdefghjkstuw]";                                   // Character 3
    $alpha4 = "[abehmnprvwxy]";                                     // Character 4
    $alpha5 = "[abdefghjlnpqrstuwxyz]";                             // Character 5

    // Expression for postcodes: AN NAA, ANN NAA, AAN NAA, and AANN NAA
    $pcexp[0] = '/^('.$alpha1.'{1}'.$alpha2.'{0,1}[0-9]{1,2})([0-9]{1}'.$alpha5.'{2})$/';

    // Expression for postcodes: ANA NAA
    $pcexp[1] =  '/^('.$alpha1.'{1}[0-9]{1}'.$alpha3.'{1})([0-9]{1}'.$alpha5.'{2})$/';

    // Expression for postcodes: AANA NAA
    $pcexp[2] =  '/^('.$alpha1.'{1}'.$alpha2.'[0-9]{1}'.$alpha4.')([0-9]{1}'.$alpha5.'{2})$/';

    // Exception for the special postcode GIR 0AA
    $pcexp[3] =  '/^(gir)(0aa)$/';

    // Standard BFPO numbers
    $pcexp[4] = '/^(bfpo)([0-9]{1,4})$/';

    // c/o BFPO numbers
    $pcexp[5] = '/^(bfpo)(c\/o[0-9]{1,3})$/';

    // Load up the string to check, converting into lowercase and removing spaces
    $postcode = strtolower("AANN NAA C/0 dks");

    // Assume we are not going to find a valid postcode
    $valid = false;

    // Check the string against the six types of postcodes
    foreach ($pcexp as $regexp)
    {
        if (preg_match($regexp,$postcode, $matches))
        {

          // Load new postcode back into the form element
          $toCheck = strtoupper ($matches[1] . ' ' . $matches [2]);

          // Take account of the special BFPO c/o format
          $toCheck = preg_replace ('/C\/O/', 'c/o ', $toCheck);

          // Remember that we have found that the code is valid and break from loop
          $valid = true;
          break;
        }
    }

    // Return with the reformatted valid postcode in uppercase if the postcode was
    // valid
    if ($valid){return true;} else {return false;};
}