Reputation: 797
I have this some code that will auto generate a string but some of the special characters is shown like: �
Code:
header("Content-type: text/html; charset=utf-8");
function RandomString($length = 10){
$chars ='0123456789abcdefghijklmnopqrstuvwxyzåäöABCDEFGHIJKLMNOPQRSTUVWXYZÅÄÖ!#¤%&()=?@£$€{}[]+';
$randString = '';
for($i = 0; $i < $length; $i++){
$randString .= $chars[rand(0, 88)];
}
echo $randString;
return $randString;
}
Upvotes: 1
Views: 743
Reputation: 1706
There are a few characters in your $chars
array, that are multibyte characters (UTF-8 to be precise). Unfortunately, PHP doesn't handle multibyte characters that well on its own.
A solution here, is to replace all the calls with a variant that supports multibyte characters. The mbstring extension provides such support.
You can replace a call as $chars[rand(0,88)]
by a call to the mb_substr function. So you get something like mb_substr($chars, rand(0, 88), 1)
.
Upvotes: 3