ob_start() is partially capturing data
I am using the following code:
PHP:
// Generate Guid
function NewGuid() {
$s = strtoupper(uniqid(rand(),true));
$guidText =
substr($s,0,8) . '-' .
substr($s,8,4) . '-' .
substr($s,12,4). '-' .
substr($s,16,4). '-' .
substr($s,20);
return $guidText;
}
// End Generate Guid
$Guid = NewGuid();
$alphabet = '123456789abcdefghijkmnopqrstuvwxyzABCDEFGHJKLMNPQRSTUVWXYZ';
function base_encode($num, $alphabet) {
$base_count = strlen($alphabet);
$encoded = '';
while ($num >= $base_count) {
$div = $num/$base_count;
开发者_JS百科 $mod = ($num-($base_count*intval($div)));
$encoded = $alphabet[$mod] . $encoded;
$num = intval($div);
}
if ($num) $encoded = $alphabet[$num] . $encoded;
return $encoded;
}
function base_decode($num, $alphabet) {
$decoded = 0;
$multi = 1;
while (strlen($num) > 0) {
$digit = $num[strlen($num)-1];
$decoded += $multi * strpos($alphabet, $digit);
$multi = $multi * strlen($alphabet);
$num = substr($num, 0, -1);
}
return $decoded;
}
// Ob start
ob_start();
echo base_encode($Guid, $alphabet); //should output: bUKpk
$theid = ob_get_contents();
ob_get_clean();
The problem:
When I echo $theid
, it shows the complete entry, but as it is being inserted into the database, only the first entry in the sequence gets inserted, for example for the entry buKPK
, only 'b' is being inserted not the rest.
check the character length in the database (i.e. if it is varchar(10) make sure you are not storing more than 10 characters in that field)
Try using ob_end_clean()
rather than ob_get_clean()
. Successive calls to ob_start() create "nested" buffering contexts; ob_get_clean()
fetches and clears the current context, but does not terminate it, so a second call to ob_start()
creates a second nested buffering context. In this case, you just want to capture the output buffer and then terminate capture context.
精彩评论