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

Predetermined text/number length #365

Closed
scottrobertson opened this Issue Jul 4, 2014 · 12 comments

Comments

Projects
None yet
2 participants
@scottrobertson
Copy link

scottrobertson commented Jul 4, 2014

Hello,

Is there a way to generate text/number to a predetermined length (not "up-to")?

I had a look for tickets relating to this, but could not find any. Apologies if it's a duplicate.

Thanks
Scott

@fzaninotto

This comment has been minimized.

Copy link
Owner

fzaninotto commented Jul 4, 2014

Yes, it's possible:

$faker->randomNumber($nbDigits = 10, $strict = true); // 624873109
$faker->lexify('??????????'); // 'gsurbwnfty'

@fzaninotto fzaninotto closed this Jul 4, 2014

@scottrobertson

This comment has been minimized.

Copy link
Author

scottrobertson commented Jul 4, 2014

Ahh awesome! Thanks

@scottrobertson

This comment has been minimized.

Copy link
Author

scottrobertson commented Jul 4, 2014

Hmm, and what about longer strings? Longer than mt_getrandmax.

@fzaninotto

This comment has been minimized.

Copy link
Owner

fzaninotto commented Jul 4, 2014

There should be no problem, except it's slow.

@scottrobertson

This comment has been minimized.

Copy link
Author

scottrobertson commented Jul 4, 2014

I get InvalidArgumentException: randomNumber() can only generate numbers up to mt_getrandmax()

@fzaninotto

This comment has been minimized.

Copy link
Owner

fzaninotto commented Jul 4, 2014

Are you on master? This should be fixed already.

@fzaninotto

This comment has been minimized.

Copy link
Owner

fzaninotto commented Jul 4, 2014

ah, no sorry it isn't fixed. Do you mean you want to generate numbers with thousands of digits?

@scottrobertson

This comment has been minimized.

Copy link
Author

scottrobertson commented Jul 4, 2014

So this is more to do with generating a string to a specific length. These strings could be like 40 chars long. And from your example, we need to generate a number and use lexify on the result of that random number.

@fzaninotto

This comment has been minimized.

Copy link
Owner

fzaninotto commented Jul 4, 2014

Do you want to generate string of random or fixed length? In the former case, use lexify. In the latter, text is the formatter to use.

@scottrobertson

This comment has been minimized.

Copy link
Author

scottrobertson commented Jul 4, 2014

fixed length. But ->text does "up to" the number of digits passed in. So if i asked for a string of 10 length, it could return a string of 8 or 9 sometime.

@fzaninotto

This comment has been minimized.

Copy link
Owner

fzaninotto commented Jul 4, 2014

So use lexify with a str_repeat('?', $length). But this won't be very performant.

@scottrobertson

This comment has been minimized.

Copy link
Author

scottrobertson commented Jul 4, 2014

Ok, thank you for your help.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment