Jump to content

random string, unknown symbol being added


jacko310592

Recommended Posts

hey guys,

i have the following code to genorate a random string of upercase and lowercase letters and numbers. the only this is that sometimes it seems to output an unknown symbol(s) as part of the string

 

here is the code:

<?php function genRandomString() {
$randomLength = mt_rand(10, 20);
    $length = $randomLength;
    $characters = “ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789”;
    for ($p = 0; $p < $length; $p++) {
        $string .= $characters[mt_rand(0, strlen($characters))];
    }
    return $string;
}
echo genRandomString(); ?>

 

 

output string examples:

v�Xv3YWXzk

0vNKRjmYqQ

OxXqZmn3Hyfi4HYvSMDh

Czl7jiqA8r4VkE

JFtoyUwnATXo�Yhi�6

 

can anyone suggest a way to prevent this?

 

thank you

thanks MatthewJ, i had just realised what was wrong just as i saw your comment xD

its because i copyed that number and letter string directly from the net (ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789), and the quotation marks surrounding it where the html type (&#8220; and &#8221;), so i replaced them for  " ",  i didnt see that error on my text editor, its only when i saw the way it had come up on this forum when i realised.

 

$characters = "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789";

Archived

This topic is now archived and is closed to further replies.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.