Warning: file_get_contents(/data/phpspider/zhask/data//catemap/5/date/2.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
如何在PHP中获取以零开头的邮政编码的第一位数字_Php_Substring - Fatal编程技术网

如何在PHP中获取以零开头的邮政编码的第一位数字

如何在PHP中获取以零开头的邮政编码的第一位数字,php,substring,Php,Substring,下面的PHP代码应该返回5的$zone。而$postcodeprefix应该是075 我认为这是行不通的,因为PHP将邮政编码视为整数而不是字符串。我尝试过以下方法: $postcode = $postcode." "; $postcode = strval($postcode); 我试过的东西都不管用 解决办法是什么 $postcode = 07558;//comes from database as a number. I can't change this. $postcode = $p

下面的PHP代码应该返回5的
$zone
。而
$postcodeprefix
应该是075

我认为这是行不通的,因为PHP将邮政编码视为整数而不是字符串。我尝试过以下方法:

$postcode = $postcode." ";
$postcode = strval($postcode);
我试过的东西都不管用

解决办法是什么

$postcode = 07558;//comes from database as a number. I can't change this.
$postcode = $postcode." "; //one of various ways I have tried to turn this into a string
$postcode = trim($postcode);

$zone = 99;
$postcodeprefix = substr($postcode,0,3);
echo "\$postcodeprefix=".$postcodeprefix."\n";
$postcodeprefixkey = substr($postcode,0,1); //this is the first postcode digit
echo "\$postcodeprefixkey=".$postcodeprefixkey."\n";
if ($postcodeprefixkey == 0) {
                //any range containing postcode which starts with 0
                if ($postcodeprefix >= 001 && $postcodeprefix <= 005) {$zone = 5;} else
                if ($postcodeprefix >= 006 && $postcodeprefix <= 009) {$zone = 6;} else
                if ($postcodeprefix >= 010 && $postcodeprefix <= 029) {$zone = 5;} else
                if ($postcodeprefix >= 030 && $postcodeprefix <= 054) {$zone = 6;} else
                if ($postcodeprefix >= 055 && $postcodeprefix <= 055) {$zone = 5;} else
                if ($postcodeprefix >= 056 && $postcodeprefix <= 059) {$zone = 6;} else
                if ($postcodeprefix >= 060 && $postcodeprefix <= 098) {$zone = 5;}
            } 
echo "\$zone=".$zone;
$postcode=07558//作为一个数字来自数据库。我不能改变这个。
$postcode=$postcode.“//我尝试过的各种方法之一就是将其转换为字符串
$postcode=trim($postcode);
$zone=99;
$postcodeprefix=substr($postcode,0,3);
echo“\$postcodeprefix=”.$postcodeprefix.\n”;
$postcodeprefixkey=substr($postcode,0,1)//这是第一个邮政编码数字
echo“\$postcodeprefixkey=“.$postcodeprefixkey.”\n”;
如果($postcodeprefixkey==0){
//任何包含以0开头的邮政编码的区域

如果($postcodeprefix>=001&&$postcodeprefix=006&&&$postcodeprefix=010&&$postcodeprefix=030&&&$postcodeprefix=055&&&$postcodeprefix=056&&$postcodeprefix=060&&$postcodeprefix则问题出在这一行:

$postcode = 07558;//comes from database as a number. I can't change this.
在这里,$postcode已经是一个八进制值

$postcode = 07558;//comes from database as a number. I can't change this.
$postcode = $postcode." "; //one of various ways I have tried to turn this into a string
$postcode = trim($postcode);

echo $postcode;
给493

将其视为一个数字,这样做比较容易:

$postcode  =  7558; // 07558 would be treated as octal value!

$postcodeC = intval($postcode / 100); // 075

if ( $postcodeC < 100 )
{
       ...
       if ($postcodeC >= 60 && $postcodeC <= 98) {$zone = 5;}
       ...
}
$postcode=7558;//07558将被视为八进制值!
$postcodeC=intval($postcode/100);//075
如果($postcodeC<100)
{
...

如果($postcodeC>=60&&$postcodeC,以下是我最终解决它的方法。我使用此技术欺骗substr()函数,使其不将$postcode重新解释为八进制数:

        $postcodeprefix = substr("x".$postcode,1,3);
        $postcodeprefixkey = substr("x".$postcode,1,1); 

$postcode=(字符串)$postcode;@slime-这不起作用。我试过了。邮政编码是字符串,而不是数字。这不起作用。你可以在这里检查:。而不是像你在评论中预期的075,你实际上得到了4。07558在PHP中被视为八进制数。我更喜欢使用字符串。是的,这是因为PHP正在解释
$postcode=07558;
作为八进制值,而不是基数10。我已经在上面更改了。正如我在问题中提到的,我无法更改数据库中的数据。因此,我无法从邮政编码中删除前导零,我也不想这样做。这会对代码的其他部分产生大小影响。然后,您可以在“从数据库中选择”中添加前缀?如
选择….CONCAT(“XX”,zip)..
它给出了
XX07558
是的,前缀就是我最后要做的…这确实有效。谢谢