ob_start() 正在部分捕获数据

发布于 2024-10-09 18:58:55 字数 1415 浏览 0 评论 0原文

我正在使用以下代码:

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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

栖迟 2024-10-16 18:58:55

检查数据库中的字符长度(即,如果是 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)

青朷 2024-10-16 18:58:55

尝试使用 ob_end_clean() 而不是 ob_get_clean()。连续调用 ob_start() 创建“嵌套”缓冲上下文; ob_get_clean() 获取并清除当前上下文,但不会终止它,因此第二次调用 ob_start() 会创建第二个嵌套缓冲上下文。在这种情况下,您只想捕获输出缓冲区,然后终止捕获上下文。

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.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文