3v4l.org

run code in 200+ php & hhvm versions
Bugs & Features
<?php /** * Generate a random string, using a cryptographically secure * pseudorandom number generator (random_int) * * For PHP 7, random_int is a PHP core function * For PHP 5.x, depends on https://github.com/paragonie/random_compat * * @param int $length How many characters do we want? * @param string $keyspace A string of all possible characters * to select from * @return string */ function random_str($length, $keyspace = '0123456789abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ') { $str = ''; $max = mb_strlen($keyspace, '8bit') - 1; for ($i = 0; $i < $length; ++$i) { $str .= $keyspace[random_int(0, $max)]; } return $str; } $str = random_str(8); var_dump(random_str(32)); var_dump($str); var_dump($str);
based on GuCqt
Output for 7.2.6
string(32) "Qa759RnjdWNLm7Av0gRsmHDloGolzANN" string(8) "b0A4X9Bf" string(8) "b0A4X9Bf"
Output for 7.2.0
string(32) "XqkVfH8A3MMUiudQaQKZq7ptT7y8TmEL" string(8) "txNXVNgs" string(8) "txNXVNgs"
Output for 7.1.10
string(32) "qs9sCy27lMEYzbYJl9qHErpns7A2eNej" string(8) "xS0mfnAo" string(8) "xS0mfnAo"
Output for 7.1.7
string(32) "5rAcyMwg5eCfN93PkQlrSKNHox8RSKWS" string(8) "q3F938de" string(8) "q3F938de"
Output for 7.1.6
string(32) "polgnrxnKJVE0fX57cvB7eqjKcWGFb8c" string(8) "KXLY00D9" string(8) "KXLY00D9"
Output for 7.1.5
string(32) "wHhtYy5nIi5XF2DzjJ3uO319XLlMSwN0" string(8) "blJGT5Ki" string(8) "blJGT5Ki"
Output for 7.1.4
string(32) "vjBMOWUCMLNasCgC8jqC1VZCBBHFOSRt" string(8) "UC1egwyA" string(8) "UC1egwyA"
Output for 7.1.3
string(32) "22GTLrqvpGde1eMZTZDQox3ngyxd47jc" string(8) "5liDvPjo" string(8) "5liDvPjo"
Output for 7.1.2
string(32) "qDl0wPK0evJ8xVUkJoXBGPk2Ui4SlTNJ" string(8) "rajYYT2B" string(8) "rajYYT2B"
Output for 7.1.1
string(32) "jD0WeqlFVozjakvdFaF0pj9o9MF9EGyN" string(8) "OICnDblK" string(8) "OICnDblK"
Output for 7.1.0
string(32) "HkvWqZWKCakwMMJwTAGhh0wPpRlDeNRX" string(8) "RW25h1SC" string(8) "RW25h1SC"
Output for 7.0.20
string(32) "qCGJPddMoiZ58ZCNlMXkmlG9SJEgdp37" string(8) "npgHWbxp" string(8) "npgHWbxp"
Output for 7.0.19
string(32) "KPoa2qY3dSWMp1vTZUkgFLzorPfBiKbP" string(8) "jDNjAzcR" string(8) "jDNjAzcR"
Output for 7.0.18
string(32) "DQkIfcui0JdFk152k0XTZRGyLN04sJP4" string(8) "0yuZV1ID" string(8) "0yuZV1ID"
Output for 7.0.17
string(32) "PnB6vTg1rEXoVRe0cGTtDssPxiReQhoh" string(8) "s69KioV2" string(8) "s69KioV2"
Output for 7.0.16
string(32) "pnJyo6ZzF7eZCpnE1lz5Ilv6isvqxdeq" string(8) "IjvG5lGK" string(8) "IjvG5lGK"
Output for 7.0.15
string(32) "U5fsuKd144qiPIRTBo2MGqBPwaMmnx8v" string(8) "2PLkE35e" string(8) "2PLkE35e"
Output for 7.0.14
string(32) "z9O3K2JOochlG4K1OVKC3JyyyYyGD337" string(8) "BfhtluTy" string(8) "BfhtluTy"
Output for 7.0.13
string(32) "MEOBoYssRnknrmt73baGX74ZuVQyAdM2" string(8) "uFbJwy0Y" string(8) "uFbJwy0Y"
Output for 7.0.12
string(32) "JF4tMSmhW7NwOngrjOMce6LIW7gVva7c" string(8) "qstJ5niJ" string(8) "qstJ5niJ"
Output for 7.0.11
string(32) "77x2eK9DFKGrZkbEgXbhoKpKBCYRMhCt" string(8) "6Au1sa5p" string(8) "6Au1sa5p"
Output for 7.0.10
string(32) "gZtxLDvZlfXUnsWc1vFC0UbA0lqz1IQk" string(8) "4I0FBECI" string(8) "4I0FBECI"
Output for 7.0.9
string(32) "pn4ISRGTBT3WQpM50xUTjCyjETuNkemr" string(8) "ofYI7TTR" string(8) "ofYI7TTR"
Output for 7.0.8
string(32) "F3NXwSzkdqbqUHqx3yV9GhxdY4ydMhHq" string(8) "8dROkwL8" string(8) "8dROkwL8"
Output for 7.0.7
string(32) "aFjexLGTilwAbshA332B52C5IEBSM2jh" string(8) "FCywGgnV" string(8) "FCywGgnV"
Output for 7.0.6
string(32) "6GD79U5FfDsB1EUczgB3hYdbQ9cs4HBc" string(8) "9wwgDNYv" string(8) "9wwgDNYv"
Output for 7.0.5
string(32) "ltqQhu6UzPCdqkPJ08SBsaPd9b3CnmRC" string(8) "vqyKCijP" string(8) "vqyKCijP"
Output for 7.0.4
string(32) "bQeJUNIKPofU9jLrwDTUMcvEQWawYjcp" string(8) "1Z2wCqTN" string(8) "1Z2wCqTN"
Output for 7.0.3
string(32) "PAtfgPVvcaluNzvBljX95y5aFi0EPYyC" string(8) "SJ7Hlsrc" string(8) "SJ7Hlsrc"
Output for 7.0.2
string(32) "mN9fMlJw8eRLkj07BGDRmJHinFrnpOJj" string(8) "hfxwTG01" string(8) "hfxwTG01"
Output for 7.0.1
string(32) "TwMs0364S2x9NHxmkwEhqGpuf1HnXMmb" string(8) "8WDo8rE0" string(8) "8WDo8rE0"
Output for 7.0.0
string(32) "IE7KAp6wArPKfRrkhegI45lZoxNh0KTW" string(8) "NSrM9ipb" string(8) "NSrM9ipb"
Output for hhvm-3.15.4
string(32) "a1GWFUDnb8klv3DK85QUGVvkWZ3NMZui" string(8) "yQ6UArEb" string(8) "yQ6UArEb"
Output for 5.6.0 - 5.6.30
Fatal error: Call to undefined function random_int() in /in/PKtvk on line 20
Process exited with code 255.