我正在使用php7cc工具https://github.com/sstalle/php7cc来帮助升级到php7。它正在返回一个错误,我不知道如何修复,因为它没有给出线号,我不完全知道它要我修复什么。我得到了
"text" : "Closing delimiter C not found",
"line" : null
JSON的错误从php7cc返回。这是返回它的页面:
<?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®ion=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
plus,在preg_replace中,也有一个Unescaper/如果添加标准定界符。这可能是导致错误的原因。
<?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;};
}