Location: PHPKode > projects > My SQL Data Browser > strMangle.php
<?php
/************************************************************/
/* $Id: strMangle.php,v 1.4 2009/01/14 09:05:33 engine Exp engine $  */
/* Copyright (c) Ohad Aloni 1990-2009. All rights reserved. */
/* Released under http://www.theora.com/license.txt (BSD) */
/************************************************************/
/************************************************************/
function addToHistogram(& $histogram, $str)
{
 $hLen = count($histogram);
 for($i=0;$i<$hLen;$i++)
  if ( $histogram[$i]['str'] == $str )
   break;
 if ( $i < $hLen ) {
  $histogram[$i]['cnt']++;
  return;
 }
 $histogram[$i]['cnt'] = 1 ;
 $histogram[$i]['str'] = $str ;
}
/************************************************************/
function msdbStrManglePick($histogram, $lastTwo)
{
 $total = 0;
 $hLen = count($histogram);
 $cands = array();
 for($i=0;$i<$hLen;$i++) {
  $candStr = $histogram[$i]['str'];
  $firstTwo = substr($candStr, 0, 2);
  /*	MSDB_ERROR("$lastTwo");	*/
  if ( strcmp($lastTwo, $firstTwo) == 0 ) {
   $cands[] = $i ;
   $total += $histogram[$i]['cnt'];
   /*	MSDB_ERROR("---$lastTwo---: Found: --".$histogram[$i]['str']."--");	*/
  }
 }
 if ( count($cands) == 1 ) {
  $ret = $histogram[$cands[0]]['str'];
  /*	MSDB_ERROR("msdbStrManglePick: Returning just $ret");	*/
  return($ret);
 }
 if ( count($cands) == 0 ) {
  // this is not an error, but no way to continue from here,
  // it will only happen in short strings, and will cut the string short.
  /*	msdbMsg("Stuck on: ---$lastTwo---");	*/
  return(false);
 }
 // > 1
 $cLen = count($cands);
 /*	MSDB_ERROR("--------CAND----------: $cLen");	*/
 /*	for($i=0;$i<count($cands);$i++) {	*/
  /*	msdb_r($histogram[$cands[$i]]);	*/
 /*	}	*/
 $choice = rand(0, $total-1);
 $pos = 0;
 /*	MSDB_ERROR("choice: $choice, pos = $pos, cLen = $cLen");	*/
 for($i=0;$i<$cLen;$i++) {
  $leapSize = $histogram[$cands[$i]]['cnt'];
  /*	MSDB_ERROR($leapSize);	*/
  /*	MSDB_ERROR("$leapSize, $choice, $pos");	*/
  if ( $choice >= $pos && $choice < ( $pos + $leapSize ) ) {
   $ret = $histogram[$cands[$i]]['str'];
   /*	MSDB_ERROR("msdbStrManglePick: Returning $ret");	*/
   return($ret);
  }
  $pos += $leapSize ;
 }
 msdbError("strMangle.php".": ". 76 .": ".("msdbStrManglePick: --- INTERNAL ERROR ---"));
 return(false);
}
/************************************************************/
function msdbStrMangle($str)
{
 $histogram = array();
 $len = strlen($str);
 for($i=0;$i<$len-2;$i++) {
  $s = substr($str, $i, 3);
  $sparse[] = $s;
  addToHistogram($histogram, $s);
 }
 /*	msdb_r($histogram);	*/
 $first = rand(0, $len-3);
 $ret = $sparse[$first];
 $lastTwo = substr($ret, 1, 2);
 /*	MSDB_ERROR("msdbStrMangle: Starting with ---$ret---");	*/
 for($i=0;$i<$len-2;$i++) { // just this many times, no use of $i
  /*	MSDB_ERROR("msdbStrManglePick: -----------------------$ret--: --lastTwo-----------------");	*/
  if ( ($pick = msdbStrManglePick($histogram, $lastTwo)) === false )
   break;
  $newChar = substr($pick, 2, 1);
  $ret = $ret.$newChar;
  $oldChar = substr($lastTwo, 1, 1);
  /*	MSDB_ERROR("changing lastTwo from $lastTwo to {$oldChar}$newChar");	*/
  $lastTwo = $oldChar.$newChar ;
 }
 /*	MSDB_ERROR("msdbStrMangle: Done");	*/
 return($ret);
}
/************************************************************/
?>
Return current item: My SQL Data Browser