{"id":284,"date":"2014-01-02T11:21:16","date_gmt":"2014-01-02T16:21:16","guid":{"rendered":"http:\/\/www.popmartian.com\/tipsntricks\/?p=284"},"modified":"2014-01-03T10:30:17","modified_gmt":"2014-01-03T15:30:17","slug":"howto-create-a-random-alpha-numeric-string-in-php","status":"publish","type":"post","link":"https:\/\/www.popmartian.com\/tipsntricks\/2014\/01\/02\/howto-create-a-random-alpha-numeric-string-in-php\/","title":{"rendered":"HOWTO: Create a random alpha-numeric string in PHP"},"content":{"rendered":"<p>Creating an alpha-numeric string in PHP is easy, but having a quick function you can call is better!<\/p>\n<p>This is very useful for things like password generators.<\/p>\n<p>To see how to do this in Python, check out this post: <a title=\"HOWTO: Create a random alpha-numeric string in Python\" href=\"http:\/\/www.popmartian.com\/tipsntricks\/2013\/12\/27\/howto-create-a-random-alpha-numeric-string-in-python\/\">\u00e2\u20ac\u00a6howto-create-a-random-alpha-numeric-string-in-python<\/a><\/p>\n<p>To see how to do this in Perl check out this post: <a title=\"HOWTO: Create a random alpha-numeric string in Perl\" href=\"http:\/\/www.popmartian.com\/tipsntricks\/2013\/12\/27\/howto-create-a-random-alpha-numeric-string-in-perl\/\">\u00e2\u20ac\u00a6howto-create-a-random-alpha-numeric-string-in-perl<\/a><\/p>\n<p><code><br \/>\n&lt;?php<br \/>\nfunction makeID($size = 6) {<br \/>\n&nbsp;&nbsp;&nbsp;&nbsp;global $makeIDpool;<br \/>\n&nbsp;&nbsp;&nbsp;&nbsp;$makeIDpool = array_merge(range(0,9), range('a','z'), range('A','Z'));<br \/>\n&nbsp;&nbsp;&nbsp;&nbsp;$append = function ($value){ global $makeIDpool; return $makeIDpool[rand(0,count($makeIDpool) - 1)]; };<br \/>\n&nbsp;&nbsp;&nbsp;&nbsp;return implode('', array_map($append, range(0,$size - 1)));<br \/>\n}<br \/>\n?&gt;<br \/>\n<\/code><\/p>\n<p>Example:<\/p>\n<p>This code sample:<\/p>\n<p><code><br \/>\nprint makeID().'&lt;br \/&gt;';<br \/>\nprint makeID(10).'&lt;br \/&gt;';<br \/>\nprint makeID(20).'&lt;br \/&gt;';<br \/>\nprint makeID(1).'&lt;br \/&gt;';<br \/>\n<\/code><\/p>\n<p>Will print something like this:<\/p>\n<p><code>KNHtkn<br \/>\n2z8ksLDSkp<br \/>\nbfn5X5jF5dTZ3itt9HWy<br \/>\nq<\/code><\/p>\n<blockquote><p>Did you find this post useful or have questions or comments?  Please let  me know!<\/p><\/blockquote>\n","protected":false},"excerpt":{"rendered":"<p>Creating an alpha-numeric string in PHP is easy, but having a quick function you can call is better! This is very useful for things like password generators. To see how to do this in Python, check out this post: \u00e2\u20ac\u00a6howto-create-a-random-alpha-numeric-string-in-python &hellip; <a href=\"https:\/\/www.popmartian.com\/tipsntricks\/2014\/01\/02\/howto-create-a-random-alpha-numeric-string-in-php\/\">Continue reading <span class=\"meta-nav\">&rarr;<\/span><\/a><\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[3,11,9],"tags":[],"class_list":["post-284","post","type-post","status-publish","format-standard","hentry","category-how-to-do-stuff","category-php","category-programming"],"aioseo_notices":[],"_links":{"self":[{"href":"https:\/\/www.popmartian.com\/tipsntricks\/wp-json\/wp\/v2\/posts\/284","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/www.popmartian.com\/tipsntricks\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/www.popmartian.com\/tipsntricks\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/www.popmartian.com\/tipsntricks\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/www.popmartian.com\/tipsntricks\/wp-json\/wp\/v2\/comments?post=284"}],"version-history":[{"count":6,"href":"https:\/\/www.popmartian.com\/tipsntricks\/wp-json\/wp\/v2\/posts\/284\/revisions"}],"predecessor-version":[{"id":295,"href":"https:\/\/www.popmartian.com\/tipsntricks\/wp-json\/wp\/v2\/posts\/284\/revisions\/295"}],"wp:attachment":[{"href":"https:\/\/www.popmartian.com\/tipsntricks\/wp-json\/wp\/v2\/media?parent=284"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/www.popmartian.com\/tipsntricks\/wp-json\/wp\/v2\/categories?post=284"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/www.popmartian.com\/tipsntricks\/wp-json\/wp\/v2\/tags?post=284"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}