ob_start() 正在部分捕获数据
我正在使用以下代码:
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;
$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();
问题:
当我 echo $theid
时,它显示完整的条目,但当它被插入数据库时,仅插入序列中的第一个条目,例如,对于条目 buKPK
,仅插入“b”,而不插入其余部分。
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;
$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.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
检查数据库中的字符长度(即,如果是 varchar(10),请确保在该字段中存储的字符不超过 10 个)
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)
尝试使用
ob_end_clean()
而不是ob_get_clean()
。连续调用 ob_start() 创建“嵌套”缓冲上下文; ob_get_clean() 获取并清除当前上下文,但不会终止它,因此第二次调用 ob_start() 会创建第二个嵌套缓冲上下文。在这种情况下,您只想捕获输出缓冲区,然后终止捕获上下文。Try using
ob_end_clean()
rather thanob_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 toob_start()
creates a second nested buffering context. In this case, you just want to capture the output buffer and then terminate capture context.