{"id":2441,"date":"2018-09-04T17:58:56","date_gmt":"2018-09-04T12:28:56","guid":{"rendered":"https:\/\/blog.pheonixsolutions.com\/?p=2441"},"modified":"2018-09-04T18:00:04","modified_gmt":"2018-09-04T12:30:04","slug":"generate-username-from-user-first-and-last-name-codeigniter-library","status":"publish","type":"post","link":"https:\/\/pheonixsolutions.com\/blog\/generate-username-from-user-first-and-last-name-codeigniter-library\/","title":{"rendered":"Generate Username from User First and Last Name \u2013  codeigniter library"},"content":{"rendered":"<p><strong>Date Posted: 04-09-2018<\/strong><\/p>\n<p>In this Post we will explain Generate Username from User First and Last Name using codeigniter library<\/p>\n<p>Step 1 : Go to folder application -&gt; libraries<\/p>\n<p>Step 2 : Create a PHP file with Unique_username_lib.php<\/p>\n<p>Step 3 : Add the following code in Unique_username_lib.php. This code is used for create the unique user name.<\/p>\n<pre class=\"EnlighterJSRAW\" data-enlighter-language=\"null\">&lt;?php\r\ndefined('BASEPATH') OR exit('No direct script access allowed');\r\n\r\nclass Unique_username_lib \r\n{\r\n    var $CI;\r\n    public function __construct($params = array())\r\n    {\r\n        $this-&gt;CI =&amp; get_instance();\r\n        $this-&gt;CI-&gt;load-&gt;database();\r\n        \r\n    }\r\n\r\n    \/\/Generate a unique username using Database\r\n    function generate_unique_username($string_name=\"\", $rand_no = \"\")\r\n    {\r\n      \/\/ echo $string_name.\"\".$rand_no;\r\n       while(true){\r\n            $username_parts = array_filter(explode(\" \", strtolower($string_name))); \/\/explode and lowercase name\r\n            $username_parts = array_slice($username_parts, 0, 2); \/\/return only first two arry part\r\n        \r\n            $part1 = (!empty($username_parts[0]))?substr($username_parts[0], 0,8):\"\"; \/\/cut first name to 8 letters\r\n            $part2 = (!empty($username_parts[1]))?substr($username_parts[1], 0,5):\"\"; \/\/cut second name to 5 letters\r\n            $part3 = ($rand_no)?rand(0, $rand_no):\"\";\r\n            \r\n            $username = $part1. str_shuffle($part2). $part3; \/\/str_shuffle to randomly shuffle all characters \r\n            \r\n            $username_exist_in_db = $this-&gt;username_exist_in_database($username); \/\/check username in database\r\n            if(!$username_exist_in_db){\r\n                return $username;\r\n            }\r\n        }\r\n    }\r\n\r\n    \/\/ User Name exist in Database.\r\n    function username_exist_in_database($username=\"\")\r\n    {\r\n      $sql = \"SELECT * FROM \".DB_PREFIX.'users'.\" WHERE username = ?\"; \r\n      $results = $this-&gt;CI-&gt;db-&gt;query($sql, array($username));\r\n      if ( $results-&gt;num_rows() &gt; 0) \r\n      {\r\n      \t# code...\r\n      \treturn true;\r\n      }\r\n      else\r\n      {\r\n      \treturn false;\r\n      }\r\n      \r\n    }\r\n}<\/pre>\n<p>Step 4: Load the liabary into controller using below command.<\/p>\n<pre class=\"EnlighterJSRAW\" data-enlighter-language=\"null\">$this-&gt;load-&gt;library('unique_username_lib');<\/pre>\n<p>Step 5: Once you load library file in above step, next calling library file in your controller method like below<\/p>\n<pre class=\"EnlighterJSRAW\" data-enlighter-language=\"null\">$this-&gt;unique_username_lib-&gt;generate_unique_username($string_name = \"Pheonix\".\"Solutions\", $rand_no = mt_rand(1000, 9999))<\/pre>\n<p>&nbsp;<\/p>\n<p>&nbsp;<\/p>\n","protected":false},"excerpt":{"rendered":"<p>Date Posted: 04-09-2018 In this Post we will explain Generate Username from User First and Last Name using codeigniter library Step 1 : Go to folder application -&gt; libraries Step 2 : Create a PHP file with Unique_username_lib.php Step 3 : Add the following code in Unique_username_lib.php. This code is&hellip; <a href=\"https:\/\/pheonixsolutions.com\/blog\/generate-username-from-user-first-and-last-name-codeigniter-library\/\" class=\"more-link read-more\" rel=\"bookmark\">Continue Reading <span class=\"screen-reader-text\">Generate Username from User First and Last Name \u2013  codeigniter library<\/span><i class=\"fa fa-arrow-right\"><\/i><\/a><\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"jetpack_post_was_ever_published":false,"_jetpack_newsletter_access":"","_jetpack_dont_email_post_to_subs":false,"_jetpack_newsletter_tier_id":0,"_jetpack_memberships_contains_paywalled_content":false,"_jetpack_memberships_contains_paid_content":false,"footnotes":""},"categories":[340],"tags":[341,273],"class_list":{"0":"post-2441","1":"post","2":"type-post","3":"status-publish","4":"format-standard","5":"hentry","6":"category-codeigniter","7":"tag-codeigniter","8":"tag-php","9":"h-entry","11":"h-as-article"},"yoast_head":"<!-- This site is optimized with the Yoast SEO plugin v27.4 - https:\/\/yoast.com\/product\/yoast-seo-wordpress\/ -->\n<title>Pheonix Solutions - We Empower Your Business Growth<\/title>\n<meta name=\"robots\" content=\"index, follow, max-snippet:-1, max-image-preview:large, max-video-preview:-1\" \/>\n<link rel=\"canonical\" href=\"https:\/\/pheonixsolutions.com\/blog\/generate-username-from-user-first-and-last-name-codeigniter-library\/\" \/>\n<meta property=\"og:locale\" content=\"en_US\" \/>\n<meta property=\"og:type\" content=\"article\" \/>\n<meta property=\"og:title\" content=\"Pheonix Solutions - We Empower Your Business Growth\" \/>\n<meta property=\"og:description\" content=\"Date Posted: 04-09-2018 In this Post we will explain Generate Username from User First and Last Name using codeigniter library Step 1 : Go to folder application -&gt; libraries Step 2 : Create a PHP file with Unique_username_lib.php Step 3 : Add the following code in Unique_username_lib.php. This code is&hellip; Continue Reading Generate Username from User First and Last Name \u2013 codeigniter library\" \/>\n<meta property=\"og:url\" content=\"https:\/\/pheonixsolutions.com\/blog\/generate-username-from-user-first-and-last-name-codeigniter-library\/\" \/>\n<meta property=\"og:site_name\" content=\"PHEONIXSOLUTIONS\" \/>\n<meta property=\"article:publisher\" content=\"https:\/\/www.facebook.com\/PheonixSolutions-209942982759387\/\" \/>\n<meta property=\"article:published_time\" content=\"2018-09-04T12:28:56+00:00\" \/>\n<meta property=\"article:modified_time\" content=\"2018-09-04T12:30:04+00:00\" \/>\n<meta property=\"og:image\" content=\"https:\/\/pheonixsolutions.com\/blog\/wp-content\/uploads\/2016\/09\/PX2.png\" \/>\n\t<meta property=\"og:image:width\" content=\"3837\" \/>\n\t<meta property=\"og:image:height\" content=\"2540\" \/>\n\t<meta property=\"og:image:type\" content=\"image\/png\" \/>\n<meta name=\"author\" content=\"admin\" \/>\n<meta name=\"twitter:card\" content=\"summary_large_image\" \/>\n<meta name=\"twitter:creator\" content=\"@pheonixsolution\" \/>\n<meta name=\"twitter:site\" content=\"@pheonixsolution\" \/>\n<meta name=\"twitter:label1\" content=\"Written by\" \/>\n\t<meta name=\"twitter:data1\" content=\"admin\" \/>\n\t<meta name=\"twitter:label2\" content=\"Est. reading time\" \/>\n\t<meta name=\"twitter:data2\" content=\"2 minutes\" \/>\n<script type=\"application\/ld+json\" class=\"yoast-schema-graph\">{\"@context\":\"https:\\\/\\\/schema.org\",\"@graph\":[{\"@type\":\"Article\",\"@id\":\"https:\\\/\\\/pheonixsolutions.com\\\/blog\\\/generate-username-from-user-first-and-last-name-codeigniter-library\\\/#article\",\"isPartOf\":{\"@id\":\"https:\\\/\\\/pheonixsolutions.com\\\/blog\\\/generate-username-from-user-first-and-last-name-codeigniter-library\\\/\"},\"author\":{\"name\":\"admin\",\"@id\":\"https:\\\/\\\/pheonixsolutions.com\\\/blog\\\/#\\\/schema\\\/person\\\/0ffa33d73c869faec2d50e79c24e3503\"},\"headline\":\"Generate Username from User First and Last Name \u2013 codeigniter library\",\"datePublished\":\"2018-09-04T12:28:56+00:00\",\"dateModified\":\"2018-09-04T12:30:04+00:00\",\"mainEntityOfPage\":{\"@id\":\"https:\\\/\\\/pheonixsolutions.com\\\/blog\\\/generate-username-from-user-first-and-last-name-codeigniter-library\\\/\"},\"wordCount\":99,\"commentCount\":0,\"publisher\":{\"@id\":\"https:\\\/\\\/pheonixsolutions.com\\\/blog\\\/#organization\"},\"keywords\":[\"codeigniter\",\"php\"],\"articleSection\":[\"Codeigniter\"],\"inLanguage\":\"en-US\",\"potentialAction\":[{\"@type\":\"CommentAction\",\"name\":\"Comment\",\"target\":[\"https:\\\/\\\/pheonixsolutions.com\\\/blog\\\/generate-username-from-user-first-and-last-name-codeigniter-library\\\/#respond\"]}]},{\"@type\":\"WebPage\",\"@id\":\"https:\\\/\\\/pheonixsolutions.com\\\/blog\\\/generate-username-from-user-first-and-last-name-codeigniter-library\\\/\",\"url\":\"https:\\\/\\\/pheonixsolutions.com\\\/blog\\\/generate-username-from-user-first-and-last-name-codeigniter-library\\\/\",\"name\":\"Pheonix Solutions - We Empower Your Business Growth\",\"isPartOf\":{\"@id\":\"https:\\\/\\\/pheonixsolutions.com\\\/blog\\\/#website\"},\"datePublished\":\"2018-09-04T12:28:56+00:00\",\"dateModified\":\"2018-09-04T12:30:04+00:00\",\"breadcrumb\":{\"@id\":\"https:\\\/\\\/pheonixsolutions.com\\\/blog\\\/generate-username-from-user-first-and-last-name-codeigniter-library\\\/#breadcrumb\"},\"inLanguage\":\"en-US\",\"potentialAction\":[{\"@type\":\"ReadAction\",\"target\":[\"https:\\\/\\\/pheonixsolutions.com\\\/blog\\\/generate-username-from-user-first-and-last-name-codeigniter-library\\\/\"]}]},{\"@type\":\"BreadcrumbList\",\"@id\":\"https:\\\/\\\/pheonixsolutions.com\\\/blog\\\/generate-username-from-user-first-and-last-name-codeigniter-library\\\/#breadcrumb\",\"itemListElement\":[{\"@type\":\"ListItem\",\"position\":1,\"name\":\"Home\",\"item\":\"https:\\\/\\\/pheonixsolutions.com\\\/blog\\\/\"},{\"@type\":\"ListItem\",\"position\":2,\"name\":\"Generate Username from User First and Last Name \u2013 codeigniter library\"}]},{\"@type\":\"WebSite\",\"@id\":\"https:\\\/\\\/pheonixsolutions.com\\\/blog\\\/#website\",\"url\":\"https:\\\/\\\/pheonixsolutions.com\\\/blog\\\/\",\"name\":\"Pheonix Solutions\",\"description\":\"We Empower Your Business Growth\",\"publisher\":{\"@id\":\"https:\\\/\\\/pheonixsolutions.com\\\/blog\\\/#organization\"},\"potentialAction\":[{\"@type\":\"SearchAction\",\"target\":{\"@type\":\"EntryPoint\",\"urlTemplate\":\"https:\\\/\\\/pheonixsolutions.com\\\/blog\\\/?s={search_term_string}\"},\"query-input\":{\"@type\":\"PropertyValueSpecification\",\"valueRequired\":true,\"valueName\":\"search_term_string\"}}],\"inLanguage\":\"en-US\"},{\"@type\":\"Organization\",\"@id\":\"https:\\\/\\\/pheonixsolutions.com\\\/blog\\\/#organization\",\"name\":\"PheonixSolutions\",\"url\":\"https:\\\/\\\/pheonixsolutions.com\\\/blog\\\/\",\"logo\":{\"@type\":\"ImageObject\",\"inLanguage\":\"en-US\",\"@id\":\"https:\\\/\\\/pheonixsolutions.com\\\/blog\\\/#\\\/schema\\\/logo\\\/image\\\/\",\"url\":\"https:\\\/\\\/pheonixsolutions.com\\\/blog\\\/wp-content\\\/uploads\\\/2016\\\/12\\\/logo.png\",\"contentUrl\":\"https:\\\/\\\/pheonixsolutions.com\\\/blog\\\/wp-content\\\/uploads\\\/2016\\\/12\\\/logo.png\",\"width\":454,\"height\":300,\"caption\":\"PheonixSolutions\"},\"image\":{\"@id\":\"https:\\\/\\\/pheonixsolutions.com\\\/blog\\\/#\\\/schema\\\/logo\\\/image\\\/\"},\"sameAs\":[\"https:\\\/\\\/www.facebook.com\\\/PheonixSolutions-209942982759387\\\/\",\"https:\\\/\\\/x.com\\\/pheonixsolution\"]},{\"@type\":\"Person\",\"@id\":\"https:\\\/\\\/pheonixsolutions.com\\\/blog\\\/#\\\/schema\\\/person\\\/0ffa33d73c869faec2d50e79c24e3503\",\"name\":\"admin\",\"image\":{\"@type\":\"ImageObject\",\"inLanguage\":\"en-US\",\"@id\":\"https:\\\/\\\/secure.gravatar.com\\\/avatar\\\/09bacc0294abee1322a23ab4bc6a0330dd4cb4df707dc9d0b0efeba6c109608b?s=96&r=g\",\"url\":\"https:\\\/\\\/secure.gravatar.com\\\/avatar\\\/09bacc0294abee1322a23ab4bc6a0330dd4cb4df707dc9d0b0efeba6c109608b?s=96&r=g\",\"contentUrl\":\"https:\\\/\\\/secure.gravatar.com\\\/avatar\\\/09bacc0294abee1322a23ab4bc6a0330dd4cb4df707dc9d0b0efeba6c109608b?s=96&r=g\",\"caption\":\"admin\"},\"sameAs\":[\"http:\\\/\\\/blog.pheonixsolutions.com\"],\"url\":\"https:\\\/\\\/pheonixsolutions.com\\\/blog\\\/author\\\/admin\\\/\"}]}<\/script>\n<!-- \/ Yoast SEO plugin. -->","yoast_head_json":{"title":"Pheonix Solutions - We Empower Your Business Growth","robots":{"index":"index","follow":"follow","max-snippet":"max-snippet:-1","max-image-preview":"max-image-preview:large","max-video-preview":"max-video-preview:-1"},"canonical":"https:\/\/pheonixsolutions.com\/blog\/generate-username-from-user-first-and-last-name-codeigniter-library\/","og_locale":"en_US","og_type":"article","og_title":"Pheonix Solutions - We Empower Your Business Growth","og_description":"Date Posted: 04-09-2018 In this Post we will explain Generate Username from User First and Last Name using codeigniter library Step 1 : Go to folder application -&gt; libraries Step 2 : Create a PHP file with Unique_username_lib.php Step 3 : Add the following code in Unique_username_lib.php. This code is&hellip; Continue Reading Generate Username from User First and Last Name \u2013 codeigniter library","og_url":"https:\/\/pheonixsolutions.com\/blog\/generate-username-from-user-first-and-last-name-codeigniter-library\/","og_site_name":"PHEONIXSOLUTIONS","article_publisher":"https:\/\/www.facebook.com\/PheonixSolutions-209942982759387\/","article_published_time":"2018-09-04T12:28:56+00:00","article_modified_time":"2018-09-04T12:30:04+00:00","og_image":[{"width":3837,"height":2540,"url":"https:\/\/pheonixsolutions.com\/blog\/wp-content\/uploads\/2016\/09\/PX2.png","type":"image\/png"}],"author":"admin","twitter_card":"summary_large_image","twitter_creator":"@pheonixsolution","twitter_site":"@pheonixsolution","twitter_misc":{"Written by":"admin","Est. reading time":"2 minutes"},"schema":{"@context":"https:\/\/schema.org","@graph":[{"@type":"Article","@id":"https:\/\/pheonixsolutions.com\/blog\/generate-username-from-user-first-and-last-name-codeigniter-library\/#article","isPartOf":{"@id":"https:\/\/pheonixsolutions.com\/blog\/generate-username-from-user-first-and-last-name-codeigniter-library\/"},"author":{"name":"admin","@id":"https:\/\/pheonixsolutions.com\/blog\/#\/schema\/person\/0ffa33d73c869faec2d50e79c24e3503"},"headline":"Generate Username from User First and Last Name \u2013 codeigniter library","datePublished":"2018-09-04T12:28:56+00:00","dateModified":"2018-09-04T12:30:04+00:00","mainEntityOfPage":{"@id":"https:\/\/pheonixsolutions.com\/blog\/generate-username-from-user-first-and-last-name-codeigniter-library\/"},"wordCount":99,"commentCount":0,"publisher":{"@id":"https:\/\/pheonixsolutions.com\/blog\/#organization"},"keywords":["codeigniter","php"],"articleSection":["Codeigniter"],"inLanguage":"en-US","potentialAction":[{"@type":"CommentAction","name":"Comment","target":["https:\/\/pheonixsolutions.com\/blog\/generate-username-from-user-first-and-last-name-codeigniter-library\/#respond"]}]},{"@type":"WebPage","@id":"https:\/\/pheonixsolutions.com\/blog\/generate-username-from-user-first-and-last-name-codeigniter-library\/","url":"https:\/\/pheonixsolutions.com\/blog\/generate-username-from-user-first-and-last-name-codeigniter-library\/","name":"Pheonix Solutions - We Empower Your Business Growth","isPartOf":{"@id":"https:\/\/pheonixsolutions.com\/blog\/#website"},"datePublished":"2018-09-04T12:28:56+00:00","dateModified":"2018-09-04T12:30:04+00:00","breadcrumb":{"@id":"https:\/\/pheonixsolutions.com\/blog\/generate-username-from-user-first-and-last-name-codeigniter-library\/#breadcrumb"},"inLanguage":"en-US","potentialAction":[{"@type":"ReadAction","target":["https:\/\/pheonixsolutions.com\/blog\/generate-username-from-user-first-and-last-name-codeigniter-library\/"]}]},{"@type":"BreadcrumbList","@id":"https:\/\/pheonixsolutions.com\/blog\/generate-username-from-user-first-and-last-name-codeigniter-library\/#breadcrumb","itemListElement":[{"@type":"ListItem","position":1,"name":"Home","item":"https:\/\/pheonixsolutions.com\/blog\/"},{"@type":"ListItem","position":2,"name":"Generate Username from User First and Last Name \u2013 codeigniter library"}]},{"@type":"WebSite","@id":"https:\/\/pheonixsolutions.com\/blog\/#website","url":"https:\/\/pheonixsolutions.com\/blog\/","name":"Pheonix Solutions","description":"We Empower Your Business Growth","publisher":{"@id":"https:\/\/pheonixsolutions.com\/blog\/#organization"},"potentialAction":[{"@type":"SearchAction","target":{"@type":"EntryPoint","urlTemplate":"https:\/\/pheonixsolutions.com\/blog\/?s={search_term_string}"},"query-input":{"@type":"PropertyValueSpecification","valueRequired":true,"valueName":"search_term_string"}}],"inLanguage":"en-US"},{"@type":"Organization","@id":"https:\/\/pheonixsolutions.com\/blog\/#organization","name":"PheonixSolutions","url":"https:\/\/pheonixsolutions.com\/blog\/","logo":{"@type":"ImageObject","inLanguage":"en-US","@id":"https:\/\/pheonixsolutions.com\/blog\/#\/schema\/logo\/image\/","url":"https:\/\/pheonixsolutions.com\/blog\/wp-content\/uploads\/2016\/12\/logo.png","contentUrl":"https:\/\/pheonixsolutions.com\/blog\/wp-content\/uploads\/2016\/12\/logo.png","width":454,"height":300,"caption":"PheonixSolutions"},"image":{"@id":"https:\/\/pheonixsolutions.com\/blog\/#\/schema\/logo\/image\/"},"sameAs":["https:\/\/www.facebook.com\/PheonixSolutions-209942982759387\/","https:\/\/x.com\/pheonixsolution"]},{"@type":"Person","@id":"https:\/\/pheonixsolutions.com\/blog\/#\/schema\/person\/0ffa33d73c869faec2d50e79c24e3503","name":"admin","image":{"@type":"ImageObject","inLanguage":"en-US","@id":"https:\/\/secure.gravatar.com\/avatar\/09bacc0294abee1322a23ab4bc6a0330dd4cb4df707dc9d0b0efeba6c109608b?s=96&r=g","url":"https:\/\/secure.gravatar.com\/avatar\/09bacc0294abee1322a23ab4bc6a0330dd4cb4df707dc9d0b0efeba6c109608b?s=96&r=g","contentUrl":"https:\/\/secure.gravatar.com\/avatar\/09bacc0294abee1322a23ab4bc6a0330dd4cb4df707dc9d0b0efeba6c109608b?s=96&r=g","caption":"admin"},"sameAs":["http:\/\/blog.pheonixsolutions.com"],"url":"https:\/\/pheonixsolutions.com\/blog\/author\/admin\/"}]}},"jetpack_featured_media_url":"","jetpack_shortlink":"https:\/\/wp.me\/p7F4uM-Dn","jetpack_sharing_enabled":true,"_links":{"self":[{"href":"https:\/\/pheonixsolutions.com\/blog\/wp-json\/wp\/v2\/posts\/2441","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/pheonixsolutions.com\/blog\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/pheonixsolutions.com\/blog\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/pheonixsolutions.com\/blog\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/pheonixsolutions.com\/blog\/wp-json\/wp\/v2\/comments?post=2441"}],"version-history":[{"count":0,"href":"https:\/\/pheonixsolutions.com\/blog\/wp-json\/wp\/v2\/posts\/2441\/revisions"}],"wp:attachment":[{"href":"https:\/\/pheonixsolutions.com\/blog\/wp-json\/wp\/v2\/media?parent=2441"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/pheonixsolutions.com\/blog\/wp-json\/wp\/v2\/categories?post=2441"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/pheonixsolutions.com\/blog\/wp-json\/wp\/v2\/tags?post=2441"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}