0

我在使用 php 序列化时遇到问题,如果包含“”,我会得到一个额外的字符空间。例如,如果它是 6 个字符,我会得到 7 个

   $episodes_count = sizeof($episode_title);
$episodes = array();
$content = '';

for ($i = 0; $i <= $episodes_count; $i++) {
    $title = htmlspecialchars($episode_title[$i], ENT_QUOTES);
    $airdate    = $episodes_airdates[$i];
    $season    = $episodes_seasons[$i];
    $number    = $episodes_numbers[$i];
    $plot    = $episodes_plot[$i];

    // check if empty
    if (!empty($title) && !empty($number ) && !empty($plot )) {
        $episodes[] = array(
            'title' => $title, 
            'airdate' => $airdate,
            'season' => $season,
            'number'   => $number,
            'plot'   => $plot,
        );
    }
}

// Serialized Episodes in case they exist, if not, remove the goal post
if ( sizeof($episodes) > 0 ) {
    $content = str_replace("'", '%',serialize($episodes));
}


update_post_meta($post_id, 'episodes', $content);
}
4

1 回答 1

2

您将一些看起来序列化的数据传递给 wordpress update_post_meta- wordpress 可能会遇到问题,并且数据在存储和检索时会被破坏。

为了防止这种情况,您可以为字符串添加前缀,使其看起来不再序列化,或者您甚至对整个字符串进行编码,例如使用base64_encode. 这将防止 wordpress 和其他组件由于编码问题而修改值。

于 2012-04-18T08:07:52.240 回答