How to Generate Random Text in PHP


This examples shows, how to generate random text or random Password with required length using PHP Script.

Example:

<?php 
  function randText($randTextLength = 6) {
    $texts = "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz";
    $textsLength = strlen($texts);
    $randTxt = "";
    for($i=0;$i<$randTextLength;$i++){
      $n=rand(0,$textsLength-1);
      $randTxt .= $texts[$n];
    }
    return $randTxt;
  }

  //Random Text with 6 Characters
  echo randText();

  echo "<br>";
  //Random Text with 15 Characters
  echo randText(15);
?>