jacko310592 Posted January 25, 2010 Share Posted January 25, 2010 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 Link to comment https://forums.phpfreaks.com/topic/189749-random-string-unknown-symbol-being-added/ Share on other sites More sharing options...
MatthewJ Posted January 25, 2010 Share Posted January 25, 2010 Maybe remove the # symbol ascii chars? Maybe one of those codes is a character not supported by the charset... I could be completely wrong though. Link to comment https://forums.phpfreaks.com/topic/189749-random-string-unknown-symbol-being-added/#findComment-1001378 Share on other sites More sharing options...
jacko310592 Posted January 25, 2010 Author Share Posted January 25, 2010 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 (“ and ”), 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"; Link to comment https://forums.phpfreaks.com/topic/189749-random-string-unknown-symbol-being-added/#findComment-1001381 Share on other sites More sharing options...
Recommended Posts
Archived
This topic is now archived and is closed to further replies.