Note that there are some explanatory texts on larger screens.

plurals
  1. POphp - help improve the efficiency of this youtube style url generator
    text
    copied!<p>After some searching I found this youtube style url generator with encryption to hide the original id... however I am hoping to improve the efficiency as it will be used a lot. So far I have improved it by 20%... can anyone help me improve it more.</p> <p>This is the original:</p> <pre><code>function alphaID($in, $to_num = false, $pad_up = false, $passKey = null) { $index = "abcdefghijklmnopqrstuvwxyz0123456789ABCDEFGHIJKLMNOPQRSTUVWXYZ"; if ($passKey !== null) { // Although this function's purpose is to just make the // ID short - and not so much secure, // with this patch by Simon Franz (http://blog.snaky.org/) // you can optionally supply a password to make it harder // to calculate the corresponding numeric ID for ($n = 0; $n&lt;strlen($index); $n++) { $i[] = substr( $index,$n ,1); } $passhash = hash('sha256',$passKey); $passhash = (strlen($passhash) &lt; strlen($index)) ? hash('sha512',$passKey) : $passhash; for ($n=0; $n &lt; strlen($index); $n++) { $p[] = substr($passhash, $n ,1); } array_multisort($p, SORT_DESC, $i); $index = implode($i); } $base = strlen($index); if ($to_num) { // Digital number &lt;&lt;-- alphabet letter code $in = strrev($in); $out = 0; $len = strlen($in) - 1; for ($t = 0; $t &lt;= $len; $t++) { $bcpow = bcpow($base, $len - $t); $out = $out + strpos($index, substr($in, $t, 1)) * $bcpow; } if (is_numeric($pad_up)) { $pad_up--; if ($pad_up &gt; 0) { $out -= pow($base, $pad_up); } } } else { // Digital number --&gt;&gt; alphabet letter code if (is_numeric($pad_up)) { $pad_up--; if ($pad_up &gt; 0) { $in += pow($base, $pad_up); } } $out = ""; for ($t = floor(log10($in) / log10($base)); $t &gt;= 0; $t--) { $a = floor($in / bcpow($base, $t)); $out = $out . substr($index, $a, 1); $in = $in - ($a * bcpow($base, $t)); } $out = strrev($out); // reverse } return $out; } </code></pre> <p>Here is my modified code so far:</p> <pre><code>function alphaID($in, $to_num = false, $pad_up = false, $passKey = null) { $index = 'abcdefghijklmnopqrstuvwxyz0123456789ABCDEFGHIJKLMNOPQRSTUVWXYZ'; $i = array('a','b','c','d','e','f','g','h','i','j','k','l','m','n','o','p','q','r','s','t','u','v','w','x','y','z','0','1','2','3','4','5','6','7','8','9','A','B','C','D','E','F','G','H','I','J','K','L','M','N','O','P','Q','R','S','T','U','V','W','X','Y','Z'); if ($passKey !== null) { // Although this function's purpose is to just make the // ID short - and not so much secure, // with this patch by Simon Franz (http://blog.snaky.org/) // you can optionally supply a password to make it harder // to calculate the corresponding numeric ID $len = strlen($index); $passhash = hash('sha256',$passKey); $passhash = (strlen($passhash) &lt; $len) ? hash('sha512',$passKey) : $passhash; for ($n=0; $n &lt; $len; $n++) { $p[] = substr($passhash, $n ,1); } array_multisort($p, SORT_DESC, $i); $index = implode($i); } $base = strlen($index); if ($to_num) { // Digital number &lt;&lt;-- alphabet letter code $in = strrev($in); $out = 0; $len = strlen($in) - 1; for ($t = 0; $t &lt;= $len; $t++) { $bcpow = bcpow($base, $len - $t); $out = $out + strpos($index, substr($in, $t, 1)) * $bcpow; } if (is_numeric($pad_up)) { $pad_up--; if ($pad_up &gt; 0) { $out -= pow($base, $pad_up); } } } else { // Digital number --&gt;&gt; alphabet letter code if (is_numeric($pad_up)) { $pad_up--; if ($pad_up &gt; 0) { $in += pow($base, $pad_up); } } $out = ""; for ($t = floor(log10($in) / log10($base)); $t &gt;= 0; $t--) { $a = floor($in / bcpow($base, $t)); $out = $out . substr($index, $a, 1); $in = $in - ($a * bcpow($base, $t)); } $out = strrev($out); // reverse } return $out; } </code></pre> <p>As you can see no major difference, only I removed the strlen from the for loops and stored it in a variable and precalculated the array for the index (a bit clumsy... but the array generation is what was making up the bulk of the computation).</p> <p>Credit where its due: here is the original authors info: * @author Kevin van Zonneveld * @author Simon Franz * @copyright 2008 Kevin van Zonneveld (kevin dot vanzonneveld dot net) * @license www dot opensource dot org/licenses/bsd-license dot php New BSD Licence * @version SVN: Release: $Id: alphaID.inc.php 344 2009-06-10 17:43:59Z kevin $ * @link kevin dot vanzonneveld dot net</p> <p>I can't post the url's as I have to low reputation :S</p>
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload