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') { $pieces = []; $max = mb_strlen($keyspace, '8bit') - 1; for ($i = 0; $i < $length; ++$i) { $pieces []= $keyspace[random_int(0, $max)]; } return implode('', $pieces); } var_dump(random_str(64)); var_dump(random_str(8, 'abcdefghijklmnopqrstuvwxyz'));
based on b4PST
Output for 7.3.0alpha4
string(64) "DkLtnc3NivX1rbofjZOOOn9NiUmMw9Tc6VQH7UN7E1NSZ5DXDGuVdg7kbdAvX959" string(8) "xpwwugzb"
Output for 7.3.0alpha3
string(64) "8mCRP5S78alxduRW0qxUeBWXP7if9YvbtWWHwlo42j511Un22qAmcLFWd52FyK1o" string(8) "bhnorqbu"
Output for 7.3.0alpha2
string(64) "FKoRbK6dS5yBAC7Rs4EoVdWLz8z2J4bTY4Roj95BgLFK4uH32L7u35ZhQ5Jo4SDo" string(8) "pusivxnf"
Output for 7.3.0alpha1
string(64) "sxVx44bi0tFzcV56tdoEuP5g72ADEHO2wsLOGtm6OxrgZ9kxDbCN3v07sa8Jv4ee" string(8) "ietiouxo"
Output for 7.2.8
string(64) "CJDQXPeWnXLlu8749hfLJnPluimEOf3wws1yGzKLUcWVDje20FRhAp4t4aX0FGId" string(8) "oodcolxk"
Output for 7.2.7
string(64) "Cf2ir9RC86PyyXzhn0Neqr3i7jF1H4LqGbnIcEGtm0kNiHJziRl8wdXxE51YvasJ" string(8) "ddjamuyu"
Output for 7.2.6
string(64) "9oZrLi85z4i7JaKFdq0NTbMrRSJrGRdtOZikS3Vm2LyS0dnIDlG0DDDn9Ew27lTR" string(8) "qmgmcsqb"
Output for 7.2.5
string(64) "cZlrvYgRTtWS0ALUmOeRfsMeTfKCZSUgssbBTGlvhi268eXc1U7cLtsCqJJzmLJf" string(8) "wdzrezpz"
Output for 7.2.4
string(64) "bgV8PfnorzYSy8RP7qtK5HkTjVT5GxgBZ6LbWzkOTFd2yFh1ZXNbKVOpgBhrJf1b" string(8) "jtxqujrl"
Output for 7.2.3
string(64) "znwMTJQsGKIRg8C87gpqkZDiiKZbamnfYytRLHHsq3mds9OwDdbQRNYnTkfozKkE" string(8) "kgevzapp"
Output for 7.2.2
string(64) "fH6uRwdYqGEVQG0pmVQWoJ5nORgs7pzWDpE6IpUfgzhVj29aR29TDu7fQehvKSEs" string(8) "ecvhasor"
Output for 7.2.1
string(64) "OrLEFnhFWR3EHyUW8nAOTRAxrXtdo2IWemqBtxKuyRE41o1usUHZewj2k3wpNlKG" string(8) "vmcfzswf"
Output for 7.2.0
string(64) "bN9yvYqeWf5B0yw6007F6rc8bfT4F1QmKVZUTY0DLwCq9y6EnNMAbjlIymVMA0gu" string(8) "pnmzaycs"
Output for 7.1.19
string(64) "TWQsKn2ctgM3VABzki0MK2PKgHzon7aKab1iBWLfOZlnhhoxZ52JeWZ2zT3IS9Nk" string(8) "fhqozuha"
Output for 7.1.18
string(64) "vQtxdnbx1eRVUXqvj9WnUTPTWxfsFKQOInFqYRseE0MCVknsjDTjAg6IrSmgK2Jd" string(8) "xyfbbojy"
Output for 7.1.17
string(64) "dNwS91TGCwRBFuCuJMLD0e8kCW1ZzrovxZBtjbDEmzO3rLTpHm6lPXpt2TWpe0uV" string(8) "uyyxpfdj"
Output for 7.1.16
string(64) "wiESujV9dyumVkYIkqL2iQYzCeCiiKCIOTpO3OXKEKsK2COvOnAiGBYDqIlmhFM6" string(8) "zjacefdr"
Output for 7.1.15
string(64) "3YFNNafTkAcS81YRJMjqaUgExIjZg4fl1TwU9wGQ8tTp37hRPv7ipalfbeZwF6fH" string(8) "hwctmltu"
Output for 7.1.14
string(64) "d0Yobji8Cf7mOjFEKACm13byZW1mcdDQAfH6UNIewdCqutoGUnFnLDfHXJ109ttF" string(8) "piksngsv"
Output for 7.1.13
string(64) "gtxNjn6AVx00YHb9mdLRUb4QW9aMz18aNdFmUYIY86XhPMhUw3crOUOnnsjjrlGr" string(8) "gsrzsmtt"
Output for 7.1.12
string(64) "stkneLsFVQr5yDCZCJ4HjNUqrVsBi9qnarO6IfcfdKiMx8URheFpgZSZZmDv7fnp" string(8) "zfnohwar"
Output for 7.1.11
string(64) "fYZEQtkYqFQT4i5P6OuLQhwiHmG7xq9Oia6n04UeaSMqdwvW33T7vLuJXqNKfv3t" string(8) "rzdcteqn"
Output for 7.1.10
string(64) "WHLSKaJzxIa7JrZ9hlJDrIrgrJNP2VAzMe9zo6yHGTucOq2XA91EUmyMLu0eVBcG" string(8) "esjpeapt"
Output for 7.1.9
string(64) "MUkMtKBUSfat33Faqj2V8kKM3KKr0GbUVHAFWxajhQYSZVscsRXIBkW8PtLFwgw6" string(8) "quxxitfa"
Output for 7.1.8
string(64) "vCPOSkgGcmu5Gr4AMwLamaKBV9j1lVcjYBeDxQd8feLcwdtXzPA1H2KNFJIFd13u" string(8) "ouzzuife"
Output for 7.1.7
string(64) "UzuvGc0jk29fWyrYfrYQatxLFvPqORxzKbA9h8nhxOfhgmxUGqgliCVtgIIV7PpB" string(8) "jlirxwvd"
Output for 7.1.6
string(64) "bh6WNioBjKSilx43CNLwQXplRv4ngxlEGhkjTI6unY0ZDnuv5lgmuH0QP61GEbn9" string(8) "nknhzoof"
Output for 7.1.5
string(64) "2ZJv4YRTruSi91WG9vOgsnZCyPYsnSs2brbdFRFhirrS2oTb0wjF4hW4d7UQNXZU" string(8) "sfvidhbe"
Output for 7.1.4
string(64) "GTqcaeX95zev0dBr5EFT0CUfpfENqdEBkr3pzbNWgmgdVMjM6ERccJ2hVCvo3coK" string(8) "mufhsxmc"
Output for 7.1.3
string(64) "Yuf4KSJkqQ2Sa9KghTPWmEV2WvwHRi2cEclZcVe81m8M9hwq2MRyksYonHTXgdqM" string(8) "sxkjrvtm"
Output for 7.1.2
string(64) "bz55Vh4aaSng3bLMMgLvpKg1ayaxCBACQt5jrwvbwqVdw70YgMCTj6x2HDz0NzeW" string(8) "cgjjpqzu"
Output for 7.1.1
string(64) "SscSgXRZsmXFbzyiNaEyoFYDe5yinmlzJWMBGvOYUZrInhTaomHukh2Cx8gZhdkY" string(8) "iqamkoai"
Output for 7.1.0
string(64) "kPwCKhw34R49ZctHcrdxwsVZfeJVFskrizN62XJ0hqZYTth55ke2nMF2GJi5JKnk" string(8) "mvdanatr"
Output for 7.0.31
string(64) "vL1SSZAxGg9zP3C7fXSCt89Lj9SfqYNdHmibpU1UXpLituqzvIAUeHO08RukFeZe" string(8) "hitzxdqa"
Output for 7.0.30
string(64) "vQprX53dab7YOxGNdw5rlZDzUesKnjVbUBMjbGcYTw6XyEoZETChmBulnGu3rToq" string(8) "zjehjtip"
Output for hhvm-3.22.0
string(64) "IlJFHXze7vYx63QeKWabdHqIPLtomjXGd3WApXWmFe1gx8JMtk14q6Qv86hbKrel" string(8) "qfecmdcg"
Output for hhvm-3.21.3
string(64) "HDHCDPOIxYScywyqhctTBylDu00ky7jORg0YPpbeLB8NzbWE948AngPlWwBd9Swb" string(8) "eggfelvs"
Output for hhvm-3.18.5
string(64) "0n4qr1qhATC1J0dsqI6MMA0D9XkQK2GZqJ4IvxNdkV695Ayy8HdK57xFjnpxU996" string(8) "wrnewwqc"
Output for 5.6.30
Fatal error: Call to undefined function random_int() in /in/Bopnv on line 20
Process exited with code 255.