Title: run_replace() function not working properly
Last modified: August 21, 2016

---

# run_replace() function not working properly

 *  Resolved [hanbr](https://wordpress.org/support/users/hanbr/)
 * (@hanbr)
 * [12 years, 8 months ago](https://wordpress.org/support/topic/run_replace-function-not-working-properly/)
 * Hi,
 * Thank you for providing this very useful plugin. Though, I think I found a bug
   in the run_replace() function. I needed to move the line:
 * `$data_to_fix = $row[$current_column];`
 * above the foreach( $replace_array as $search_for => $replace_with) loop and also
   add:
 * `$data_to_fix = $edited_data;`
 * …at the end of the same loop. Else I think it will only be the last search string
   in the replace_array that will cause an update in each column.
 * I also had some problems with UTF-8 coding and swedish signs. It helped if I 
   fetched data with:
 *     ```
       global $wpdb;
       $data = $wpdb->get_results( $SQL, ARRAY_A );
       ```
   
 * …instead of
 * `$data = mysql_db_query($db, $SQL, $cid);`
 * …and updated it with:
 * `$wpdb->query($UPDATE_SQL);`
 * …instead of:
 * `$result = mysql_db_query($db,$UPDATE_SQL,$cid);`
 * …else the unserialze function was not able to unserialize the fetched data.
 * Br, Hans
 * [http://wordpress.org/plugins/ns-cloner-site-copier/](http://wordpress.org/plugins/ns-cloner-site-copier/)

Viewing 4 replies - 1 through 4 (of 4 total)

 *  Plugin Author [Never Settle](https://wordpress.org/support/users/neversettle/)
 * (@neversettle)
 * [12 years, 8 months ago](https://wordpress.org/support/topic/run_replace-function-not-working-properly/#post-4106834)
 * Thanks, Hans! We’ll play with those mods and see what we get.
 *  Plugin Author [Never Settle](https://wordpress.org/support/users/neversettle/)
 * (@neversettle)
 * [12 years, 8 months ago](https://wordpress.org/support/topic/run_replace-function-not-working-properly/#post-4106908)
 * Looking at your first recommended change we can’t see how that would help. Maybe
   there is something else going on in your environment? But we can’t reproduce 
   the issue / and the current replace code has been stable for many versions.
 * However, your second issue was helpful to us in finding a burried UTF-8 / DB 
   charset bug. The reason your fix worked is because $wpdb->query() uses the charset
   constant defined in wp-config.php. We just released an update to set this explicitly
   and preserve the existing code to ensure stability.
 * Many thanks for your contributions!
 *  Thread Starter [hanbr](https://wordpress.org/support/users/hanbr/)
 * (@hanbr)
 * [12 years, 8 months ago](https://wordpress.org/support/topic/run_replace-function-not-working-properly/#post-4106917)
 * Thanks for adding the UTF-8 / DB charset capability!
 * Though, I still think it is a bug in the search and replace loop. When you have
   the special case where you have a match on let say two of the search strings 
   in the same column, then I think you will get an update SQL with same column 
   twice:
 * UPDATESQL = UPDATE <table> SET columnX = “edited_data_after_first_ replace’, 
   columnX = ‘edited_data_after_second_replace’
 * The ‘edited_data_after_second_replace’ is the result after a replace on $data_to_fix
   which is set to $row[$current_column] in the beginning of the loop which means
   that the first replace will have no effect.
 * This might be a rare case but I noticed such column for a plugin that saved all
   it option data serialized in one column.
 * BR,
    Hans
 *  Plugin Author [Never Settle](https://wordpress.org/support/users/neversettle/)
 * (@neversettle)
 * [12 years, 8 months ago](https://wordpress.org/support/topic/run_replace-function-not-working-properly/#post-4106918)
 * Sure thing, Hans, and many thanks for the additional input / clarification on
   the replace scenario you were seeing. Off the top of my head, the search-replace
   is serial array safe and iterates through the entire table set once per search/
   replace term… or wait… maybe that’s the issue… actually, it probably iterates
   over the data only once, and if it finds a match/replace in a column it moves
   on even if there is another match for another term still in that column. Interesting.
   That could be it. But then your solution might also break down in other scenarios
   too (e.g. 3 matches in one column?). Hmmm. This is important because we’re planning
   to build a much more robust search and replace add-on that will support virtually
   unlimited replacements and will allow a powerful cloning + re-configuring on 
   the fly solution. But I know that many plugins store lots of settings in a single
   serial array column so there’s a strong likelihood that we’ll have to rework 
   that loop to make sure every replacement check runs against each column… which
   will also impact performance I suspect. Sorry, kind of thinking outloud. In fact,
   this might also explain other rare quirks we’ve seen with unexpected results 
   in some theme settings… maybe the same issue if those themes are storing their
   entire set of settings in a single serial array col.
 * Thanks for your help and persistence! We’ll look at this a lot more closely. 
   That scenario example was very helpful. Much appreciated!

Viewing 4 replies - 1 through 4 (of 4 total)

The topic ‘run_replace() function not working properly’ is closed to new replies.

 * ![](https://ps.w.org/ns-cloner-site-copier/assets/icon-256x256.gif?rev=2323308)
 * [NS Cloner - Site Copier](https://wordpress.org/plugins/ns-cloner-site-copier/)
 * [Frequently Asked Questions](https://wordpress.org/plugins/ns-cloner-site-copier/#faq)
 * [Support Threads](https://wordpress.org/support/plugin/ns-cloner-site-copier/)
 * [Active Topics](https://wordpress.org/support/plugin/ns-cloner-site-copier/active/)
 * [Unresolved Topics](https://wordpress.org/support/plugin/ns-cloner-site-copier/unresolved/)
 * [Reviews](https://wordpress.org/support/plugin/ns-cloner-site-copier/reviews/)

## Tags

 * [unserialize](https://wordpress.org/support/topic-tag/unserialize/)

 * 4 replies
 * 2 participants
 * Last reply from: [Never Settle](https://wordpress.org/support/users/neversettle/)
 * Last activity: [12 years, 8 months ago](https://wordpress.org/support/topic/run_replace-function-not-working-properly/#post-4106918)
 * Status: resolved