Base36 dahil büyük harf gibi kodlama

2 Cevap

Ben URL'leri kısaltmak için base36 kullanıyorum. Ben bir blog girişi bir kimliği var ve daha küçük yapmak için base36 için bu kimliği dönüştürmek. Base36 sadece küçük harfleri içerir. Nasıl büyük harfler içerebilir? Ben base64_encode kullanırsanız aslında dize uzun yapar.

Thanks, Max

2 Cevap

Harfleri (alt ve üst harf hem de) ve örneğin bu iki makaleleri numarasını içeren kısa URL'ler oluşturmak için kaynak kod örnekleri bulabilirsiniz:

İşte bu ikinci yazıda kullanılan kod kısmıdır (quoting):

$codeset = "0123456789abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ";
$base = strlen($codeset);
$n = 300;
$converted = "";

while ($n > 0) {
  $converted = substr($codeset, ($n % $base), 1) . $converted;
  $n = floor($n/$base);
}

echo $converted; // 4Q

Ve oldukça kolay bir fonksiyonu bu kozalayabilirsiniz - düşünmeye tek şey $n, bir parametre olarak alınacak olmasıdır:

function shorten($n) {
    $codeset = "0123456789abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ";
    $base = strlen($codeset);
    $converted = "";
    while ($n > 0) {
      $converted = substr($codeset, ($n % $base), 1) . $converted;
      $n = floor($n/$base);
    }
    return $converted;
}

Ve bu şekilde çağırıyor:

$id = 123456;
$url = shorten($id);
var_dump($url);

Alacağınız:

string 'w7e' (length=3)

(You can also add some other characters, if needed -- depending on what you want to get in your URLs)


Edit after the comment :

(Ben kısalma kodu var olan) ikinci yazı ile okuma, un-kısalma yapar kodunu bulabilirsiniz.

Bir işlevi bu kodu Encapsulating da zor olmamalı, ve sana böyle bir şey alabilirsiniz:

function unshorten($converted) {
    $codeset = "0123456789abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ";
    $base = strlen($codeset);
    $c = 0;
    for ($i = strlen($converted); $i; $i--) {
      $c += strpos($codeset, substr($converted, (-1 * ( $i - strlen($converted) )),1)) 
            * pow($base,$i-1);
    }
    return $c;
}

Ve bir kısaltılmış-url ile çağırarak:

$back_to_id = unshorten('w7e');
var_dump($back_to_id);

Alacak:

int 123456
function dec2any( $num, $base=62, $index=false ) {

    // Parameters:
    //   $num - your decimal integer
    //   $base - base to which you wish to convert $num (leave it 0 if you are providing $index or omit if you're using default (62))
    //   $index - if you wish to use the default list of digits (0-1a-zA-Z), omit this option, otherwise provide a string (ex.: "zyxwvu")

    if (! $base ) {
        $base = strlen( $index );
    } else if (! $index ) {
        $index = substr( "0123456789abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ" ,0 ,$base );
    }
    $out = "";
    for ( $t = floor( log10( $num ) / log10( $base ) ); $t >= 0; $t-- ) {
        $a = floor( $num / pow( $base, $t ) );
        $out = $out . substr( $index, $a, 1 );
        $num = $num - ( $a * pow( $base, $t ) );
    }
    return $out;
}

Utanmadan (base_convert() sadece 32 tabanına kadar çalışır) PHP'nin base_convert() sayfada bir yorumcu ödünç.