Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Compatibility with multibyte characters #21

Open
Trainmaster opened this issue Mar 30, 2014 · 1 comment
Open

Compatibility with multibyte characters #21

Trainmaster opened this issue Mar 30, 2014 · 1 comment

Comments

@Trainmaster
Copy link

$factory = new RandomLib\Factory;
$generator = $factory->getGenerator(new SecurityLib\Strength(SecurityLib\Strength::MEDIUM));

var_dump($generator->generateString(8, 'abc'));
// string(8) "bbcaaacb"

var_dump($generator->generateString(8, 'äöü'));
// string(8) "ö¼Ã¤Ã¼¶"

var_dump($generator->generateString(8, 'a'));
// string(0) ""

var_dump($generator->generateString(8, 'ä'));
// string(8) "¤¤Ã¤¤¤Ã¤"
@ircmaxell
Copy link
Owner

In general, this will not be solvable unless significant constraints are put on the character set. As in make that a 3rd option to the method. This would require abstracting mb_string and intl, as we're not sure if they are available, and which one is.

Which means it's definitely possible, but it's not trivial. I'd suggest in the mean time doing it yourself:

$string = "";
$chars = "äöü";
$len = mb_strlen($chars, "utf-8");
for ($i = 0; $i < 8; $i++) {
    $string .= $chars[$generator->generateInt(0, $len)];
}

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants