Skip to content

Instantly share code, notes, and snippets.

@cobyan
Created June 12, 2016 16:40
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save cobyan/f4914b5bc507b9891a4d746def97b172 to your computer and use it in GitHub Desktop.
Save cobyan/f4914b5bc507b9891a4d746def97b172 to your computer and use it in GitHub Desktop.
This function will replace a given text with a new value in a serialized string
<?php
/**
* This function will replace a given text with a new value
* in a serialized string
*
* @param string $input serialized value to seek
* @param string $search text to be replaced
* @param string $replace replacement text
* @return string new serialized string
*/
function serialize_replace($input, $search, $replace) {
$ua = unserialize($input);
$replace_callback = function (&$value, $key, $data) {
if(!is_int($value))
$value = str_replace($data['search'], $data['replace'], $value);
};
array_walk_recursive($ua, $replace_callback , ['search' => $search, 'replace' => $replace]);
$b = serialize($ua);
return $b;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment