0

I am trying write a PHP function that returns a random string of a given length. I wrote this:

<?
function generate_string($lenght) {
    $ret = "";
    for ($i = 0; $i < $lenght; $i++) {
        $ret .= chr(mt_rand(32,126));
    }
    return $ret;
}

echo generate_string(150);
?>

The above function generates a random string, but the length of the string is not constant, ie: one time it is 30 characters, the other is 60 (obviously I call it with the same length as input every time). I've searched other examples of random string generators, but they all use a base string to pick letters. I am wondering why this method is not working properly.

Thanks!

10
  • What does your function call look like? Commented Apr 19, 2012 at 14:41
  • Show us the code that is calling the function (including the lines before and after the call). BTW, length is spelled incorrectly (not that PHP cares.) Commented Apr 19, 2012 at 14:42
  • Also, minor quibble: it's "length" and not "lenght" - maybe you're confusing this somewhere? Commented Apr 19, 2012 at 14:43
  • <? function generate_string($lenght) { $ret = ""; for ($i = 0; $i < $lenght; $i++) { $ret .= chr(mt_rand(32,126)); } return $ret; } echo generate_string(150); ?> Commented Apr 19, 2012 at 14:43
  • @Komut, please put it into your post, not on a comment...and you're passing a length of 150? Commented Apr 19, 2012 at 14:44

3 Answers 3

3

Educated guess: you attempt to display your plain text string as HTML. The browser, after being told it's HTML, handles it as such. As soon as a < character is generated, the following characters are rendered as an (unknown) HTML tag and are not displayed as HTML standards mandate.

Fix:

echo htmlspecialchars(generate_string(150));
Sign up to request clarification or add additional context in comments.

Comments

0

This is the conclusion i reached after testing it a while : Your functions works correctly. It depends on what you do with the randomly generated string. If you are simply echo-ing it, then it might generate somthing like <ck1ask which will be treated like a tag. Try eliminating certain characters from being concatenated to the string.

Comments

0

This function will work to generate a random string in PHP

function getRandomString($maxlength=12, $isSpecialChar=false)
{
    $randomString=null;
   //initalise the string include lower case, upper case and numbers
   $charSet = "abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789";

   //if required special character to include, please set $isSpecialchar= 1 or true
  if ($isSpecialChar) $charSet .= "~@#$%^*()_±={}|][";

  //loop for get specify length character with random characters
  for ($i=0; $i<$maxlength; $i++) $randomString .= $charSet[(mt_rand(0,   (strlen($charSet)-1)))];

   //return the random string
   return $randomString;
}
//call the function set value you required to string length default:12
$random8char=getRandomString(8);
echo $random8char;

Source: Generate random string in php

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.