I am searching for PHP component that generates random string from regex. I've searched through this forum, but found only PERL etc. solutions (Random string that matches a regexp). Is there such an open source class?
Asked
Active
Viewed 6,001 times
6
-
1regexes are for matching strings that have already been generated, not generating them. – Jonathan Kuhn Jan 09 '14 at 21:02
-
2Generally questions asking for libraries etc. are off topic for SO; you might reword your question referencing the PERL solutions you found and how to convert to PHP. – brandonscript Jan 09 '14 at 21:02
-
@Jonathan Kuhn: In other words I want to generate random string that will match a selected regex. – Michal Jan 09 '14 at 21:04
-
possible duplicate of [Random string that matches a regexp](http://stackoverflow.com/questions/205411/random-string-that-matches-a-regexp) – KeyNone Jan 09 '14 at 21:06
-
If you know the rules of the regex, in php you would probably just create a list of allowable characters and build your random string by selecting random characters from that list. – Jonathan Kuhn Jan 09 '14 at 21:07
-
@Basti M ... Ok, so what is the PHP equivalent for String::Random mentioned in http://stackoverflow.com/questions/205411/random-string-that-matches-a-regexp?lq=1 ? – Michal Jan 09 '14 at 21:08
-
2look at [this answer](http://stackoverflow.com/a/15969145/1807643), it mentions a php lib – KeyNone Jan 09 '14 at 21:09
-
@Basti M: Oh I was so blind, thank you very much – Michal Jan 09 '14 at 21:10
-
@r3mus google `php random string regex`, third link :P – KeyNone Jan 09 '14 at 21:11
2 Answers
4
I'm not familiar I wasn't (until now) familiar with any php libraries that can do this, and converting something like xeger would be a tremendous undertaking.
Instead, you could simply do something like this:
function random($length)
{
$random_string = "";
$valid_chars = "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789!@#$%^&*()_+-=[]{}\\|";
$num_valid_chars = strlen($valid_chars);
for ($i = 0; $i < $length; $i++)
{
$random_pick = mt_rand(1, $num_valid_chars);
$random_char = $valid_chars[$random_pick-1];
$random_string .= $random_char;
}
return $random_string;
}
echo random(20); //generates a random string, 20 characters long.

brandonscript
- 68,675
- 32
- 163
- 220
-
Simplified this answer: `$validCharacters = range(0, 9); //select your char using range()` Then `$randVal = implode('', array_rand ( $validCharacters , $length ));` – Ng Sek Long Jan 08 '21 at 05:25
4
A library called ReverseRegex can be found here.
As of January 2014 the project seems not to be dead.
Usage seems to be as simple as
$lexer = new Lexer('[a-z]{10}');
$gen = new SimpleRandom(10007);
$result = '';
$parser = new Parser($lexer,new Scope(),new Scope());
$parser->parse()->getResult()->generate($result,$gen);
echo $result;
Give it a regex, give it a random seed and generate your string.
The above example, taken from the github-site, generates a ten character long random string consisting only of letters.
(originally found in this answer)