Tips and tricks
Find and Replace Strings in Serialized Strings
The problem with serialized inputs is that – so basically the size of the string is remembered:
:6:”source”;s:62:”http://localhost/wordpress/wp-content/uploads/2015/03/test.m4v”;s:
If you url is shorter / longer then the new size must be updated which makes it difficult for FIND AND REPLACE
Basically you need to unserialize the array then serialize it again if you want to find and replace
I made a tool for that:
And the code if anyone is wondering
<?php ?> <style> pre{ background-color: #444444; color: #dddddd; font-size: 11px; white-space: normal; padding: 15px; word-break: break-all; } </style> <form method="post"> <div class="setting-label"> <h5>Serialized Data</h5> <textarea rows="20" name="serializeddata"><?php if(isset($_POST['serializeddata'])){ echo $_POST['serializeddata']; } ?></textarea> </div> <div class="setting-label"> <h5>Search</h5> <input type='text' name="search" value="<?php if(isset($_POST['search'])){ echo $_POST['search']; } ?>"/> </div> <div class="setting-label"> <h5>Replace With</h5> <input type='text' name="replace" value="<?php if(isset($_POST['replace'])){ echo $_POST['replace']; } ?>"/> </div> <br> <button type="submit">Replace</button> </form><?php if(isset($_POST['serializeddata'])){ $string_data = $_POST['serializeddata']; $arr = unserialize($string_data); include_once "dzs_functions.php"; replace_in_matrix($_POST['search'],$_POST['replace'],$arr); // print_r($arr); ?><h3>Output</h3> <pre><?php echo htmlentities(serialize($arr)); ?></pre><?php }
if (!function_exists('replace_in_matrix')) { function replace_in_matrix($arg1, $arg2, &$argarray) { foreach ($argarray as &$newi) { //print_r($newi); if (is_array($newi)) { foreach ($newi as &$newj) { if (is_array($newj)) { foreach ($newj as &$newk) { if (!is_array($newk)) { $newk = str_replace($arg1, $arg2, $newk); } } } else { $newj = str_replace($arg1, $arg2, $newj); } } } else { $newi = str_replace($arg1, $arg2, $newi); } } } }