if (!function_exists('ckStrReplace')) {
function ckStrReplace($cookieValue,$replacements)
{
if(empty($cookieValue)) return '';
if(empty($replacements) || !is_array($replacements)) return '';
// 第一步:将 cookie 字符串拆分为键值对数组
$cookiePairs = explode(';', $cookieValue);
// 创建一个关联数组来存储键值对
$cookieArray = [];
foreach ($cookiePairs as $pair) {
$pair = trim($pair); // 去除多余的空格
if ($pair) {
list($key, $value) = explode('=', $pair, 2);
$cookieArray[$key] = $value;
}
}
// 第二步:根据 replacements 数组更新或者添加相应的值
foreach ($replacements as $key => $newValue) {
// 如果数组中有这个键,进行替换
$cookieArray[$key] = $newValue;
}
// 第三步:重新组装成 cookie 字符串
$newCookieValue = '';
foreach ($cookieArray as $key => $value) {
$newCookieValue .= $key . '=' . $value . '; ';
}
// 去掉最后的多余分号和空格
return rtrim($newCookieValue, '; ');
}
}
$str = 'cna=Z10vHxyLLhkCAXPgqFaTMN9A; t=0dfd9e12691b5e686d8e406981bc179f; cookie2=113f243906b3776465e8fc246291d43d; xlly_s=1; _samesite_flag_=true; _tb_token_=548dd75bd1b39; unb=2209368547703; _hvn_lgc_=77; mtop_partitioned_detect=1; _m_h5_tk=518ab9b7ade30d1634c92f423962eb36_1729138819303; _m_h5_tk_enc=548aa63b41127caaeb4e9bc65e796a49; sdkSilent=1729216940928; sgcookie=E100MANykuWklerydA%2BMl9VIKBDu93fi6%2FAkc52GgwunEqZm9s%2F4QR3w70efJdK0OjoqaSSsURjd8rmp0UbOxRuxqtfyCuinoz28cswnbcjUUfk%3D; csg=96db0be1; havana_lgc2_77=eyJoaWQiOjIyMDkzNjg1NDc3MDMsInNnIjoiM2I1OTA0MDEzYTljZTcwZTRkZDkwYTQ1YzkzYjgxYzEiLCJzaXRlIjo3NywidG9rZW4iOiIxcEprcmNFY3FXa3l3U1RnY2hhT1U0dyJ9; havana_lgc_exp=1731722711974; isg=BHd3EOiN3F5lmFlq1jMGY7UyBm3BPEuetKiDGckl5cateJK60QzK7iOTWtgmkCMW; tfstk=gtErVp23SDVj2CSshxnEQC-n2_n-D0f1tkGIKJ2nFbcoAahntJFxdHw3VWPU_7-7OJf8TklmM2aWVWNeT7NjZ3wSNSl31SZI-6n7gSa0k_C-O8MUGr4Aw_EUpHk3KWB-OTQbe8nKx11_YGw8eWEySDXjrIXnLv1_7crXm8nKx19XxNsae50nOCBExtomdYJox2cl3jDsi3DHqvAc3vhmx30nrqAmevkk-vV3ntkKiXDL382H0AglCB0NDGfWA4cgEj-HbdHrrewWRh-a0Yuzx8cVL9rqU4lilG5xelygElHUvOYETzzuNxFFbso0gy2nuo1kfXaaCk3Uq9pS9zMtqYPhjFG0u-q-5-pPI-ija-DTu1--VRz0VqEGG3FUC7rE57scEWnoKW3TA1K-ZJ2TXPiNYFogg-qt-kCvvXabKko8sst492jrHBHmRXqLzBYnrxHq1tW4O4UiIC35k2YpJqmt3f6SFeLKrWMq1TgBJe3mtxl1Fw5..';
dump($str);
$newStr = ckStrReplace($str,[
'unb' => '测试',
'cookie2' => '测试2',
'x5sec' => 'test2'
]);
dump($newStr);