Why PHP serialized data breaks after a database search-and-replace
Changing a domain name with SQL REPLACE() or sed corrupts PHP serialized strings because their byte lengths no longer match. Here is how to spot it and repair it safely.
On this page
You moved a site to a new domain, ran a find-and-replace on the database dump, and now settings are gone, widgets are empty, or the log says unserialize(): Error at offset 1234 of 5678 bytes. The data is not lost. Its length prefixes are wrong.
The cause: serialize() stores byte lengths
PHP’s serialize() writes every string with its length in bytes:
a:1:{s:7:"siteurl";s:23:"https://old-domain.test";}
Replace old-domain.test with new-domain.example and the string grows from 23 to 26 bytes, but the prefix still says s:23. unserialize() reads 23 bytes, expects "; and finds xam instead, so it fails and returns false. Applications such as WordPress then treat the option as missing.
Multibyte characters cause the same problem when a tool counts characters instead of bytes: é is one character but two bytes in UTF-8.
Spot the broken string
Paste the value into the serialized data inspector. It parses the string without executing anything and lists each string whose declared length does not match its contents, for example “String declares 23 bytes but contains 26”. It can then rebuild the value with every length recalculated.
In PHP, you can find the offset yourself:
$value = $row['option_value'];
if ($value !== 'b:0;' && @unserialize($value, ['allowed_classes' => false]) === false) {
echo "Broken: ", error_get_last()['message'], PHP_EOL;
}
Repair it properly
The safe repair is to unserialize, replace, and serialize again, so PHP computes the lengths:
function replace_in_serialized(string $value, string $from, string $to): string
{
$data = unserialize($value, ['allowed_classes' => false]);
if ($data === false && $value !== 'b:0;') {
return $value; // leave genuinely broken values for manual review
}
array_walk_recursive($data, function (&$item) use ($from, $to) {
if (is_string($item)) $item = str_replace($from, $to, $item);
});
return serialize($data);
}
allowed_classes => false stops PHP from instantiating objects found in the data, which matters because unserialize() on untrusted input can trigger code in magic methods. Objects come back as __PHP_Incomplete_Class; if your data contains objects you need, list the exact classes instead of false.
For WordPress, wp search-replace 'old' 'new' does this for you, and handles nested serialized values. Run it with --dry-run first.
If the data is already broken
A regex that recalculates s:N:"…"; lengths works for simple values, but it can mis-split strings that themselves contain ";. Prefer a parser that walks the structure, such as the inspector above, and keep a backup of the original dump.
Prevent it next time
- Never search-and-replace a raw SQL dump that contains serialized PHP.
- Store new structured data as JSON (
json_encode()), which has no length prefixes. PHP 8.5 soft-deprecates__sleep()and__wakeup(), another nudge away from serialized objects. - Keep domains out of stored data where you can, and build absolute URLs at runtime.