Welcome Guest, Not a member yet? Register   Sign In
string helper
#1

[eluser]mr daniel[/eluser]
Hi guys.

In my directory. localhost/system/helpers/string_helper.php

I have
Code:
if ( ! function_exists('random_string'))
{
function random_string($type = 'alnum', $len = 8)
{
  switch($type)
  {
   case 'basic' : return mt_rand();
    break;
   case 'alnum' :
   case 'numeric' :
   case 'nozero' :
   case 'alpha' :

     switch ($type)
     {
      case 'alpha' : $pool = 'abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ';
       break;
      case 'alnum' : $pool = '0123456789abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ';
       break;
      case 'numeric' : $pool = '0123456789';
       break;
      case 'nozero' : $pool = '';
       break;
     }

     $str = '';
     for ($i=0; $i < $len; $i++)
     {
      $str .= substr($pool, mt_rand(0, strlen($pool) -1), 1);
     }
     return $str;
    break;
   case 'unique' :
   case 'md5'  :

      return md5(uniqid(mt_rand()));
    break;
   case 'encrypt' :
   case 'sha1' :

      $CI =& get_instance();
      $CI->load->helper('security');

      return do_hash(uniqid(mt_rand(), TRUE), 'sha1');
    break;
  }
}
}

I want to replace the
Code:
case 'nozero' : $pool = '';
       break;
with something that has three words and three numbers in it.


So like
Code:
case 'word' : $pool = '(my)(fun)(zone)(12)(24)(37)';
       break;

Is there a way to group letters and numbers together? I need this for my website as the words must relate to the site in question!
#2

[eluser]Aken[/eluser]
I would just create /application/helpers/MY_string_helper.php Then, add your own random string function in it (with a different name than the existing random string function), and use that. Then, you can still load the string helper to access your function.
#3

[eluser]mr daniel[/eluser]
hmmm okay thank-you! Big Grin

But is it possible to group my words together? because currently they just come out as symbols Sad
#4

[eluser]Aken[/eluser]
Make your pool an array instead of a string, then pull random array elements to generate your random string.
#5

[eluser]mr daniel[/eluser]
Would it be possible to get an example? Sad
I'm just learning how to code this properly
#6

[eluser]Aken[/eluser]
Basic example:

Code:
$pool = array('my', 'fun', 'zone', '12', '24', '37');

$str = '';

// Grab 3 random array keys from $pool and loop them.
foreach (array_rand($pool, 3) as $pool_key)
{
$str .= $pool[$pool_key];
}

echo $str;
#7

[eluser]mr daniel[/eluser]
So I tried to create
Code:
switch ($type)
     {
      case 'alpha' : $pool = 'abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ';
       break;
      case 'alnum' : $pool = '0123456789abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ';
       break;
      case 'numeric' : $pool = '0123456789';
       break;
      case 'nozero' : $pool = '123456789';
       break;
      case 'fourlet' : $pool = array('my', 'fun', 'zone', '12', '24', '37');
       break;
     }

     $str = '';
     for ($i=0; $i < $len; $i++)
     {
      $str .= substr($pool, mt_rand(0, strlen($pool) -1), 1);
     }
     return $str;


and also attempted it using the code you provided yet it still failed Sad I'll keep attempting
#8

[eluser]Aken[/eluser]
You can't use substr() on an array. As I said, you should create your own function, don't try and hack it into one that already exists. Especially if you're just getting started with PHP - learn by doing simple things first. And always use PHP.net to look up functions that you don't know, to see what they do and how you're supposed to use them.
#9

[eluser]mr daniel[/eluser]
You're a legend. Thank-you very much for your help Smile




Theme © iAndrew 2016 - Forum software by © MyBB