Loading... 写了个简单的脚本来替换以前存储在数据库中的PHP序列化数据。 比如从 HTTP 到 HTTPS 的协议升级,或者将某个域名替换成另一个域名。 运行后会出现一个简单的 HTML 表单,允许输入序列化数据并提交。提交后,脚本会处理数据并显示替换后的序列化字符串。 ```php <?php /** * 替换被PHP序列化后的数据 * @author Dracowyn * @since 2024/7/25 下午3:09 */ // 序列化数据转换为数组 function unserialize_to_array($str) { if (!is_string($str) || ($data = unserialize($str)) === false) { return false; } return $data; } // 替换数据 function replaceData($input) { // 为了方便替换,先将数组转换为json字符串 $data = json_encode($input); $replacements = [ 'http:' => 'https:', 'old something' => 'new something', ]; // 替换数据 foreach ($replacements as $search => $replace) { $data = str_replace($search, $replace, $data); } // 将json字符串转换为数组 return json_decode($data); } // 表单 function form($input): string { // 将用户输入的数据反序列化为数组 $data = unserialize_to_array($input); if ($data === false) { return '数据格式错误'; } // 替换数据 $data = replaceData($data); // 将数组序列化为字符串 return serialize($data); } // 输出html表单供用户提交需要处理的数据 echo '<form method="post" action="">'; if (isset($_POST['input'])) { echo '<textarea name="input" rows="20" cols="200">' . $_POST['input'] . '</textarea>'; } else { echo '<textarea name="input" rows="20" cols="200"></textarea>'; } echo '<br>'; echo '<input type="submit" value="提交">'; if (isset($_POST['input'])) { $input = $_POST['input']; $serialized = form($input); echo '<br>'; echo '<textarea name="output" rows="20" cols="200">' . $serialized . '</textarea>'; } ``` Last modification:July 25, 2024 © Allow specification reprint Support Appreciate the author AliPayWeChat Like If you think my article is useful to you, please feel free to appreciate