40

有什么方法可以使用 laravel faker 生成假字符串?

就像在 laravel 中我们生成最多 20 个字符的字符串..

 str_random(20);
4

5 回答 5

61

Faker 提供了几种方法,可以让你用随机字符替换给定字符串中的占位符:

  • lexify - 接受给定的字符串并替换 ? 带有随机字母
  • asciify - 接受给定的字符串并用随机的 ascii 字符替换 *
  • numerify - 接受给定的字符串并用随机数字替换 #
  • bothify - 结合了 lexify 和 numerify

您可以尝试使用其中之一,具体取决于您对所需随机字符串的要求。asciify使用最大的字符集作为替换,因此使用它最有意义。

以下将为您提供20 个 ASCII 字符的随机字符串

$faker->asciify('********************')
于 2018-03-24T13:44:47.453 回答
47

替代生成没有特殊字符的字符串。

$faker->regexify('[A-Za-z0-9]{20}')
于 2019-06-11T10:18:36.873 回答
4
$faker->text($maxNbChars = 50);
于 2021-02-02T14:19:02.970 回答
2

使用 Faker \Provider\en_US\Text

<?php

realText($maxNbChars = 200, $indexSize = 2) // "And yet I wish you could manage it?) 'And what are they made of?' Alice asked in a shrill, passionate voice. 'Would YOU like cats if you were never even spoke to Time!' 'Perhaps not,' Alice replied."
于 2018-03-24T13:43:12.813 回答
1
$faker->text()
// generates 50 char by default: "Aut quo omnis placeat eos omnis eos."

$faker->text(10);
// generates 10 char by default: "Labore."

所有文本似乎都是一个或多个带有空格的拉丁伪句子,并且(每个句子的)末尾总是一个点。

于 2021-07-13T13:19:32.487 回答