php庫Faker

Faker

  • License : MIT
  • Source Code
  • Allo點評:Faker是一個很神奇的項目,會自動生成擬真的數據,包括用戶資料、長文本、IP、日期等等,在網站上線前測試時很是好用。

github地址:php

https://github.com/fzaninotto/Fakergit

基本使用:github

Use Faker\Factory::create() to create and initialize a faker generator, which can generate data by accessing properties named after the type of data you want.dom

<?php // require the Faker autoloader require_once '/path/to/Faker/src/autoload.php'; // alternatively, use another PSR-0 compliant autoloader (like the Symfony2 ClassLoader for instance) // use the factory to create a Faker\Generator instance $faker = Faker\Factory::create(); // generate data by accessing properties echo $faker->name; // 'Lucy Cechtelar'; echo $faker->address; // "426 Jordy Lodge // Cartwrightshire, SC 88120-6700" echo $faker->text;

Even if this example shows a property access, each call to $faker->name yields a different (random) result. This is because Faker uses __get() magic, and forwards Faker\Generator->$property calls toFaker\Generator->format($property).ide

<?php for ($i=0; $i < 10; $i++) { echo $faker->name, "\n"; }

Formatters

Each of the generator properties (like nameaddress, and lorem) are called "formatters". A faker generator has many of them, packaged in "providers". Here is a list of the bundled formatters in the default locale.測試

Faker\Provider\Base

randomDigit             // 7
randomDigitNotNull      // 5
randomNumber($nbDigits = NULL) // 79907610
randomFloat($nbMaxDecimals = NULL, $min = 0, $max = NULL) // 48.8932
numberBetween($min = 1000, $max = 9000) // 8567
randomLetter            // 'b'
randomElements($array = array ('a','b','c'), $count = 1) // array('c')
randomElement($array = array ('a','b','c')) // 'b'
numerify($string = '###') // '609'
lexify($string = '????') // 'wgts'
bothify($string = '## ??') // '42 jz'
相關文章
相關標籤/搜索