2013-07-04 2 views
0

입력 된 사용자 이름을 사용할 수없는 경우 사용자 이름을 제안하는 PHP 함수를 만들고 싶습니다. 예를 들어 가 입력 한 사용자 이름이 '빙고'이고 사용할 수없는, 시스템이 이름을 생성하기위한이PHP의 사용자 이름 제안

bi_n_go 
go-nbi 
b-n_gio 
b-ng-oi 
... 

규칙 같은 사용자 이름 목록을 제시해야 할 것은 다음과 같습니다

  • 최소 길이 6
  • 사용자 이름은 최대 3 개의 기호 (하이픈, 밑줄 만 가능)
  • 으로 시작해야합니다. 사용자 이름은 영숫자로 시작하고 영숫자로 끝나야합니다.

도움이나 제안은 매우 유용 할 것입니다. 감사. 다음 코드

+0

@Deepu : I 몇 가지 정규식과 함께 str_shuffle을 시도했는데 몇몇 포럼에서 가져 왔지만 필요한 것은 얻을 수 없다 : (.) ​​제안 사항이 있습니까? – LX7

+1

'bing_o', 'b_ing_o' 등의 사용자 이름을 사용한다고 생각하십니까? 'bi_ng_o '는 그것들을 구별하는데 도움이 될 것입니까? – Voitcus

+0

최소 길이 - '6', 최대 - '3'. 큰! – vikingmaster

답변

2

시도 :

<?php 
//mutates given user name, producing possibly incorrect username 
function mutate($uname) 
{ 
    $x = str_split($uname); 

    //sort with custom function, that tries to produce only slightly 
    //random user name (as opposed to completely shuffling) 
    uksort($x, function($a, $b) 
    { 
     $chance = mt_rand(0, 3); 
     if ($chance == 0) 
     { 
      return $b - $a; 
     } 
     return $a - $b; 
    }); 

    //insert randomly dashes and underscores 
    //(multiplication for getting more often 0 than 3) 
    $chance = mt_rand(0, 3) * mt_rand(0, 3)/3.; 
    for ($i = 0; $i < $chance; $i ++) 
    { 
     $symbol = mt_rand() & 1 ? '-' : '_'; 
     $pos = mt_rand(0, count($x)); 
     array_splice($x, $pos, 0, $symbol); 
    } 
    return join('', $x); 
} 

//validates the output so you can check whether new user name is correct 
function validate($uname) 
{ 
    //does not start nor end with alphanumeric characters 
    if (!preg_match('/^[a-zA-Z0-9].*[a-zA-Z0-9]$/', $uname)) 
    { 
     return false; 
    } 
    //does contain more than 3 symbols 
    $noSymbols = preg_replace('/[^a-zA-Z0-9]+/', '', $uname); 
    if (strlen($uname) - strlen($noSymbols) > 3) 
    { 
     return false; 
    } 
    //shorter than 6 characters 
    if (strlen($uname) < 6) 
    { 
     return false; 
    } 
    return true; 
} 

사용 예제 :

$uname = 'bingo'; 
$desired_num = 5; 
$sug = []; 
while (count($sug) < $desired_num) 
{ 
    $mut = mutate($uname); 
    if (!validate($mut)) 
    { 
     continue; 
    } 
    if (!in_array($mut, $sug) and $mut != $uname) 
    { 
     $sug []= $mut; 
    } 
} 
print_r($sug); 

예 출력 :

Array 
(
    [0] => i-g-obn 
    [1] => bi-gno 
    [2] => bi_ngo 
    [3] => i-bnog 
    [4] => bign-o 
) 
+0

이것이 실제로 필요한 것입니다. 고맙습니다 rr- :) – LX7

1

이 내 생성 기능입니다. 나는 그것이 사용자의 입력을 셔플하여 사용자를 제안 할 수 PHP와 MySQL을 통해 확인한 후 당신에게

echo stringGenerator("bingo"); 
function stringGenerator($str) 
{ 
$middleStr = $str."-_"; 
$first = $str[rand(0, strlen($str)-1)]; 
for($i=0;$i<4;$i++) 
{ 
    $middle .= $middleStr[rand(0, strlen($middleStr))]; 
} 
$last = $str[rand(0, strlen($str)-1)]; 

return $first.$middle.$last; 
} 
0

을 도움이되기를 바랍니다 :

$str = 'abcdef'; 
for($i=0;$i<=3;$i++){ 
$shuffled = str_shuffle($str).'</br>'; 
echo $shuffled; 
} 

출력 :

bfdcea 
    cdfbae 
    adefcb 
    beacfd