PHPackages                             vielhuber/magicreplace - PHPackages - PHPackages  [Skip to content](#main-content)[PHPackages](/)[Directory](/)[Categories](/categories)[Trending](/trending)[Leaderboard](/leaderboard)[Changelog](/changelog)[Analyze](/analyze)[Collections](/collections)[Log in](/login)[Sign up](/register)

1. [Directory](/)
2. /
3. [Database &amp; ORM](/categories/database)
4. /
5. vielhuber/magicreplace

ActiveLibrary[Database &amp; ORM](/categories/database)

vielhuber/magicreplace
======================

A search/replace class with zero overhead.

1.6.6(1mo ago)122312MITPHPPHP &gt;=8.1CI passing

Since Mar 20Pushed 1mo ago1 watchersCompare

[ Source](https://github.com/vielhuber/magicreplace)[ Packagist](https://packagist.org/packages/vielhuber/magicreplace)[ RSS](/packages/vielhuber-magicreplace/feed)WikiDiscussions main Synced 4w ago

READMEChangelogDependencies (2)Versions (67)Used By (2)

[![build status](https://github.com/vielhuber/magicreplace/actions/workflows/ci.yml/badge.svg)](https://github.com/vielhuber/magicreplace/actions)[![GitHub Tag](https://camo.githubusercontent.com/ae8de8a3d12f5bab58968afa2bbe8efefde019e5fcdbedd7629846c1532a9777/68747470733a2f2f696d672e736869656c64732e696f2f6769746875622f762f7461672f7669656c68756265722f6d616769637265706c616365)](https://github.com/vielhuber/magicreplace/tags)[![Code Style](https://camo.githubusercontent.com/1540f8ce219727155ab62506c77b818b720421c22c4cf0b18a5f160942132e2d/68747470733a2f2f696d672e736869656c64732e696f2f62616467652f636f64655f7374796c652d7073722d2d31322d6666363962342e737667)](https://www.php-fig.org/psr/psr-12/)[![License](https://camo.githubusercontent.com/4ba302d8653e58779863f65ab7f0ce8e69aba85cee4be822cfb4e2e984d77354/68747470733a2f2f696d672e736869656c64732e696f2f6769746875622f6c6963656e73652f7669656c68756265722f6d616769637265706c616365)](https://github.com/vielhuber/magicreplace/blob/main/LICENSE.md)[![Last Commit](https://camo.githubusercontent.com/00aa7b8dd0442acd8038a9521e26e92b67b6b8154eada361f0bcafec06d38422/68747470733a2f2f696d672e736869656c64732e696f2f6769746875622f6c6173742d636f6d6d69742f7669656c68756265722f6d616769637265706c616365)](https://github.com/vielhuber/magicreplace/commits)[![PHP Version Support](https://camo.githubusercontent.com/975a197ebba0d4aad20fb7a8c6579572108bd51bea62840a820a75afc1e75a4b/68747470733a2f2f696d672e736869656c64732e696f2f7061636b61676973742f7068702d762f7669656c68756265722f6d616769637265706c616365)](https://packagist.org/packages/vielhuber/magicreplace)[![Packagist Downloads](https://camo.githubusercontent.com/5d97b27b4f5282795b423b8e4e38cac210ee4876844ce5038eafbc54d2822f27/68747470733a2f2f696d672e736869656c64732e696f2f7061636b61676973742f64742f7669656c68756265722f6d616769637265706c616365)](https://packagist.org/packages/vielhuber/magicreplace)

✨ magicreplace ✨
================

[](#-magicreplace-)

magicreplace is a search replace tool for migrating databases.

Intro
-----

[](#intro)

### The problem

[](#the-problem)

When moving databases, usually the url environment also changes. If the URL is hardcoded in the database (like WordPress [does](https://make.wordpress.org/core/handbook/contribute/design-decisions/#absolute-versus-relative-urls)), those URLs have to be changed. If you now do a search and replace on your entire database to change the URLs, you will corrupt data that has been serialized. Just try out

```
unserialize(str_replace('www.foo.tld', 'www.barrr.tld', serialize('url=www.foo.tld')));
```

and you will get an ugly error.

### There already exist cool tools that solve this issue, for example...

[](#there-already-exist-cool-tools-that-solve-this-issue-for-example)

- [Velvet Blues Update URLs](https://wordpress.org/plugins/velvet-blues-update-urls/)
- [Better Search Replace](https://wordpress.org/plugins/better-search-replace/)
- [Suchen &amp; Ersetzen](https://de.wordpress.org/plugins/search-and-replace/)
- [WP Migrate DB](https://de.wordpress.org/plugins/wp-migrate-db/)
- [WP-CLI's search-replace](http://wp-cli.org/commands/search-replace/)
- [Search-Replace-DB](https://github.com/interconnectit/Search-Replace-DB)
- [SerPlace](http://pixelentity.com/wordpress-search-replace-domain/)

### How is magicreplace different from those tools?

[](#how-is-magicreplace-different-from-those-tools)

- Fast (~5sec runtime on 100mb database file with 300.000 rows)
- Lightweight: Only &lt;10kb in size
- Works also on big files with small memory limit settings
- File based: Does not need a database or a wp installation - works on plain (sql) files
- Local usage: Does not need a remote server or a webservice
- Multi replace: Does multiple replaces
- Considers edge cases: Can handle objects and even references
- Ignores classes that are not available at runtime
- Can be used either with the command line or as a class
- Acts carefully: If serialization fails, nothing is changed
- Never changes data (out of bound ints are preserved, auto generated dates are not updated)
- Does its work in junks to overcome php limits
- Supports replacements in special base64 strings (e.g. in BeTheme)

### Disclaimer

[](#disclaimer)

This does not release you from taking backups. Use this script at your own risk!

Command line
------------

[](#command-line)

### Requirements

[](#requirements)

##### Mac

[](#mac)

```
brew install coreutils

```

##### Windows

[](#windows)

Runs out of the box with [WSL/WSL2](https://docs.microsoft.com/en-us/windows/wsl/about)/[Cygwin](https://cygwin.com/install.html).

##### Linux

[](#linux)

–

### Installation

[](#installation)

```
wget https://raw.githubusercontent.com/vielhuber/magicreplace/main/src/magicreplace.php

```

### Usage

[](#usage)

```
php magicreplace.php input.sql output.sql search-1 replace-1 search-2 replace-2

```

Class
-----

[](#class)

### Installation

[](#installation-1)

```
composer require vielhuber/magicreplace

```

### Usage

[](#usage-1)

```
