【问题标题】:php fzaninotto Faker - How to Generate Only 5 Digit Zip Code?php fzaninotto Faker - 如何仅生成 5 位邮政编码?
【发布时间】:2016-03-19 09:23:02
【问题描述】:

有没有办法使用fzaninotto's Faker 生成 5 位数的邮政编码/邮政编码?

他为此提供的函数$faker->postcode 不提供限制其大小的参数,因此它通常给出 9 位邮政编码而不是 5,例如 10278-4159 而不是 10278。这对于播种是有问题的一个数据库,因为我的 sql 数据库的 zipcode 列是 INT(5)。

我知道我可以只使用 Faker 生成一个随机的 5 位数号码,但我想知道是否有一种生成 5 位数邮政编码的本地方式。

【问题讨论】:

  • 你可以使用substr()缩短返回的邮政编码 -> $postcode = (strlen($faker->postcode) > 5) ? substr($faker->postcode,0,5) : $faker->postcode;
  • 邮政编码不应该是整数。我所在城市的邮政编码以0 开头,所以这会引起问题。领先0问题的演示; sqlfiddle.com/#!9/814ba/1...你不能只取前 5 个字符吗?
  • "因为我的 sql 数据库的邮政编码列是 INT(5)" 危险!危险,威尔罗宾逊!你要把02108 变成2108 并且必须左填充它才能显示。邮政编码不是整数。

标签: php zipcode postal-code faker


【解决方案1】:

我发现的一种解决方法是使用总是返回 5 位数字的地址类别的邮政编码。

您可以拨打Address::postcode(),而不是$faker->postcode

不要忘记包含use Faker\Provider\Address;

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2013-02-08
    • 1970-01-01
    • 1970-01-01
    • 2014-09-17
    • 2015-02-24
    • 2014-05-23
    • 2012-07-10
    相关资源
    最近更新 更多