9951 explained code solutions for 126 technologies


php-fakerHow can I generate HTML text using Laravel Faker?


To generate HTML text using Laravel Faker, you can use the html() method. This method will generate a random HTML string with the specified number of tags.

For example:

$faker = Faker\Factory::create();
echo $faker->html(3);

Output example

<b><h3>Omni</h3></b><p><em>Nam</em> voluptas.</p><i>Qui</i>

The html() method takes one parameter, which is an integer that specifies the number of HTML tags to generate. The tags can be any combination of <b>, <i>, <em>, <strong>, <h1>, <h2>, <h3>, <h4>, <h5>, <h6>, <p> or <a> tags.

The generated HTML string will also contain random text between the tags.

Code explanation

  • $faker = Faker\Factory::create();: This creates a Faker instance.
  • echo $faker->html(3);: This calls the html() method on the Faker instance, with the parameter 3 to specify the number of HTML tags to generate.

Helpful links

Edit this code on GitHub