PHP : Class Pengacak Kombinasi String v0.2 - Elang Sakti
Download Ebook Belajar Arduino PDF, Arduino untuk pemula
Jasa Pembuatan Program Arduino, pemrograman Arduino
# Hack Your Skills! to be Professional Mechatronics

PHP : Class Pengacak Kombinasi String v0.2

Tidak ada komentar
Script ini adalah perbaikan dari class php pada artikel sebelumnya. Fungsi dari class ini adalah untuk menemukan kombinasi dari karakter yang kita tentukan. Untuk contoh kasus, akan kita bahas pada tulisan selanjutnya. :)

Berikut ini adalah kelas utamanya :

<?php
/* 
 * String Combination Generator v0.2
 * coder haripinter - hari\at/elangsakti.com
 * Created : Wed, Jan 22 - 2014, 06.00 WIB. Malang - Indonesia
 * Recoded : Sat, Jan 25 - 2014, 20.01 WIB. Malang - Indonesia
 *   Changed: - Restructuring code
 *      - Changing format from 321 to 123
 *      - Ascending result
 *      - Faster if use combine format, ex. xxx65
 * 
 * $Format : format string
 *           ex. xxx => all string will combine
 *               xx4 => all string combine except 4, 4 is default value
 *
 * $Chars : list of character to generate
 *           ex. '0123456789' or 'abcde... etc'
 * 
 * $Result : the result
 */
class SCGenerator{
 var $Result;
 var $Format;
 var $Chars;
 
 var $Len;
 var $Clean_format;
 
 function setFormat($str){
     $this->Format = $str;
 }
 
 function setChars($str){
     $this->Chars = $str;
 }
 
 function get_cleanformat(){
  $format = $this->Format;
  $this->Len = strlen($format);
  $len = $this->Len;
  
  $clean = 0;
  $format_tmp = '';
  for($a=0; $a<$len; $a++){
   if(strtolower($format[$a])=='x'){
    $clean++;
    $format_tmp .= 'x';
   }else{
    $format_tmp .= $format[$a];
   }
  }
  $this->Format = $format_tmp;
  $this->Clean_format = $clean;
  return $clean;
 }
 
    function Generate(){
  $chars = $this->Chars;
  $format = $this->Format;
  $formatlen = $this->get_cleanformat();
        $charlen = strlen($chars);
  
  if($formatlen<1 or $charlen<1){
   echo "You must define both Chars and Format first!";
   exit();
  }
  
        $pis = array();
        for($a=0; $a<$formatlen; $a++){
            $pis[$a] = 0;
        }
        
        $result = array();
        $loop = false;
        do{
            $tmp = '';
         $xyz = true;
         for($x=0; $x<$formatlen; $x++){
          if($chars[$pis[$x]]==''){
           $tmp .= $chars[0];
          }else{
           $tmp .= $chars[$pis[$x]];
          }
          $xyz = (($chars[$pis[$x]]==$chars[$charlen]) && $xyz);
          $loop = $xyz;
          if($chars[$pis[$x]]==$chars[$charlen]){
           $pis[$x+1]++;
           $pis[$x] = 0;
          }
          if($x==0){
           $pis[$x]++;
          }
         }
         if(!$xyz){
          $result[] = $this->reverse($tmp);
         }
        }while(!$loop);
  
  $this->Result = $result;
        return $this->Result;
    }
 
 function reverse($strs){
  $format = $this->Format;
  $cleanf = $this->Clean_format;
  $len = $this->Len;
  
  $tmp = '';
  for($a=$cleanf; $a>=0; $a--){
   $tmp .= $strs[$cleanf--];
  }
  
  $tmq = '';
  if($len>$cleanf){
   $x = 0;
   for($a=0; $a<$len; $a++){
    if($format[$a]=='x'){
     $tmq .= $tmp[$x]; 
     $x++;
    }else{
     $tmq .= $format[$a];
    }
   }
  }else{
   $tmq = $tmp;
  }
  return $tmq;
 }
}
?>

Berikut ini adalah contoh cara menggunakan class generator :

<?php
include_once('SCGenerator2.php');

$scg2 = new SCGenerator();

// -- number
$char = '0123456789';
$format = 'xx1';

echo "\n\nVersi 2\n";
$start = microtime(true);
$scg2->setChars($char);
$scg2->setFormat($format);
$scg2->Generate();
$stop = microtime(true);
$secs = ($stop-$start);
print_r($scg2->Result);
echo $secs." seconds\n";

$format = 'xx';

echo "\n\nVersi 2\n";
$start = microtime(true);
$scg2->setFormat($format);
$scg2->Generate();
$stop = microtime(true);
$secs = ($stop-$start);
print_r($scg2->Result);
echo $secs." seconds\n";

?>

Outputnya :

Versi 2
Array
(
    [0] => 001
    [1] => 011
    [2] => 021
    [3] => 031
    [4] => 041
    [5] => 051
    [6] => 061
    [7] => 071
    [8] => 081
    [9] => 091
    [10] => 101
    [11] => 111
    [12] => 121
    [13] => 131
    [14] => 141
    [15] => 151
    [16] => 161
    [17] => 171
    [18] => 181
    [19] => 191
    [20] => 201
    [21] => 211
    [22] => 221
    [23] => 231
    [24] => 241
    [25] => 251
    [26] => 261
    [27] => 271
    [28] => 281
    [29] => 291
    [30] => 301
    [31] => 311
    [32] => 321
    [33] => 331
    [34] => 341
    [35] => 351
    [36] => 361
    [37] => 371
    [38] => 381
    [39] => 391
    [40] => 401
    [41] => 411
    [42] => 421
    [43] => 431
    [44] => 441
    [45] => 451
    [46] => 461
    [47] => 471
    [48] => 481
    [49] => 491
    [50] => 501
    [51] => 511
    [52] => 521
    [53] => 531
    [54] => 541
    [55] => 551
    [56] => 561
    [57] => 571
    [58] => 581
    [59] => 591
    [60] => 601
    [61] => 611
    [62] => 621
    [63] => 631
    [64] => 641
    [65] => 651
    [66] => 661
    [67] => 671
    [68] => 681
    [69] => 691
    [70] => 701
    [71] => 711
    [72] => 721
    [73] => 731
    [74] => 741
    [75] => 751
    [76] => 761
    [77] => 771
    [78] => 781
    [79] => 791
    [80] => 801
    [81] => 811
    [82] => 821
    [83] => 831
    [84] => 841
    [85] => 851
    [86] => 861
    [87] => 871
    [88] => 881
    [89] => 891
    [90] => 901
    [91] => 911
    [92] => 921
    [93] => 931
    [94] => 941
    [95] => 951
    [96] => 961
    [97] => 971
    [98] => 981
    [99] => 991
)
0.0029339790344238 seconds


Versi 2
Array
(
    [0] => 00
    [1] => 01
    [2] => 02
    [3] => 03
    [4] => 04
    [5] => 05
    [6] => 06
    [7] => 07
    [8] => 08
    [9] => 09
    [10] => 10
    [11] => 11
    [12] => 12
    [13] => 13
    [14] => 14
    [15] => 15
    [16] => 16
    [17] => 17
    [18] => 18
    [19] => 19
    [20] => 20
    [21] => 21
    [22] => 22
    [23] => 23
    [24] => 24
    [25] => 25
    [26] => 26
    [27] => 27
    [28] => 28
    [29] => 29
    [30] => 30
    [31] => 31
    [32] => 32
    [33] => 33
    [34] => 34
    [35] => 35
    [36] => 36
    [37] => 37
    [38] => 38
    [39] => 39
    [40] => 40
    [41] => 41
    [42] => 42
    [43] => 43
    [44] => 44
    [45] => 45
    [46] => 46
    [47] => 47
    [48] => 48
    [49] => 49
    [50] => 50
    [51] => 51
    [52] => 52
    [53] => 53
    [54] => 54
    [55] => 55
    [56] => 56
    [57] => 57
    [58] => 58
    [59] => 59
    [60] => 60
    [61] => 61
    [62] => 62
    [63] => 63
    [64] => 64
    [65] => 65
    [66] => 66
    [67] => 67
    [68] => 68
    [69] => 69
    [70] => 70
    [71] => 71
    [72] => 72
    [73] => 73
    [74] => 74
    [75] => 75
    [76] => 76
    [77] => 77
    [78] => 78
    [79] => 79
    [80] => 80
    [81] => 81
    [82] => 82
    [83] => 83
    [84] => 84
    [85] => 85
    [86] => 86
    [87] => 87
    [88] => 88
    [89] => 89
    [90] => 90
    [91] => 91
    [92] => 92
    [93] => 93
    [94] => 94
    [95] => 95
    [96] => 96
    [97] => 97
    [98] => 98
    [99] => 99
)
0.0026800632476807 seconds

Semoga bermanfaat :)

Written by ElangSakti
PHP : Class Pengacak Kombinasi String v0.2
Bahasan: Script ini adalah perbaikan dari class php pada artikel sebelumnya . Fungsi dari class ini adalah untuk menemukan kombinasi dari karakter ...
Published at Sabtu, 25 Januari 2014, Updated at Sabtu, 25 Januari 2014
Reviewed by dr. on
Rating: 4.7

Tidak ada komentar :

Posting Komentar