BLOG

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);
            }
        }
    }

}

Leave a comment

Your email address will not be published

You may use these HTML tags and attributes: <a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code> <del datetime=""> <em> <i> <q cite=""> <s> <strike> <strong>

16 − 15 =

{"type":"main_options","images_arr":"'#ffffff'","enable_ajax":"off","soundcloud_apikey":"","bg_isparallax":"off","bg_slideshow_time":"0","bg_transition":"slidedown","site_url":"http:\/\/digitalzoomstudio.net","theme_url":"http:\/\/digitalzoomstudio.net\/wp-content\/themes\/qucreative\/","blur_ammount":"26","width_column":"50","width_section_bg":"","width_gap":"30","border_width":"0","border_color":"#ffffff","translate_leave_a_comment":"Leave a comment","translate_leave_a_comment_to":"Leave a comment to","is_customize_preview":"off","width_blur_margin":"30","gallery_w_thumbs_autoplay_videos":"off","enable_native_scrollbar":"on","content_enviroment_opacity":"30","menu_enviroment_opacity":"70"}
{"type":"darkfull"}