49

is there any way or method to generate fake string using laravel faker ?

like in laravel we generate string upto 20 chars..

 str_random(20);
Vishal
  • 611
  • 1
  • 8
  • 8

5 Answers5

73

Faker offers a couple of methods that let you replace placeholders in a given string with random characters:

  • lexify - takes given string and replaces ? with random letters
  • asciify - takes given string and replaces * with random ascii characters
  • numerify - takes given string and replaces # with random digits
  • bothify - combines the lexify and numerify

You could try to use one of them, depending on the requirements you have for that random string you need. asciify uses the largest set of characters as replacement so using that one makes most sense.

The following will give you a random string of 20 ascii characters:

$faker->asciify('********************')
jedrzej.kurylo
  • 39,591
  • 9
  • 98
  • 107
66

Alternate for generate string without special chars.

$faker->regexify('[A-Za-z0-9]{20}')
Matthias
  • 1,130
  • 10
  • 8
  • You can use like [A-z] to simplify the [A-Za-z] in your regex. – francisco Jul 05 '21 at 15:49
  • 3
    @francisco these are not the same. A-z is a character range of ascii 65 to 122. You should use /[A-Za-z]/ or /[a-z]/i, unless you wish to include these characters: [ \ ] ^ _ ` – DevAntoine Jul 06 '21 at 15:26
4
$faker->text($maxNbChars = 50);
shaedrich
  • 5,457
  • 3
  • 26
  • 42
benoit1521
  • 61
  • 2
4
$faker->text()
// generates 50 char by default: "Aut quo omnis placeat eos omnis eos."

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

All texts seems to be one or more latin pseudo-sentences with spaces and always a dot in the end (of each sentence).

Sliq
  • 15,937
  • 27
  • 110
  • 143
2

uze 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."
Supun Praneeth
  • 3,087
  • 2
  • 30
  • 33