/SearchReplace

A search and replace database tool for PHP (beta)

Primary LanguageHTMLGNU General Public License v3.0GPL-3.0

Build Status Code Coverage Scrutinizer Code Quality SensioLabsInsight

Search and Replace

SearchReplace is a PHP library aimed at executing search and replace queries on the database and even handles json, serialized data, and base64 replacements.

Basic Example

Without composer

require 'src/SearchReplace.php';

$request = new SearchReplace();
$request->setDatabase($host, $username, $password, $database)
        ->search($needle, $search_regex)
        ->replace($needle, $search_regex)
        ->execute();

Basic Example (Composer)

Using composer autoload

// require autoloader
require 'vendor/autoload.php';

// use namespace
use SearchReplace/SearchReplace;

// .. somewhere in your file

$request = new SearchReplace();
$request->setDatabase()
        ->search($needle, $search_regex)
        ->replace($needle, $search_regex)
        ->execute();

Advanced Example

Create a custom database resource that we can pass around. Specify tables to exclude during search execution.

// create custom database connection
$db_instance = new SearchReplaceMySQLDatabase($host, $username, $password, $database);
$exclude_tables = [
    'craft_entries'
    'wp_posts',
    'wp_postmeta'
];

$request = new SearchReplace();
$request->setDatabase($db_instance)
        ->search($needle, $search_regex)
        ->replace($needle, $search_regex)
        ->includeAllTables(true)
        ->excludeTables($exclude_tables)
        ->setTableOffset(50)
        ->setTableLimit(10)
        ->execute();

Methods

setDatabase( mixed $resource_or_host [, string $username, string $password, string $database ] )

You can pass in a database resource or you can allow the class to create a new database resource by providing the full database credentials.

Parameters

$resource_or_host (string) or (resource)
  • Expects either a database resource or if you want the class to create a new connection, provide the host address as a string.

Database resource example:

$mysqli = new mysqli('localhost', 'user', 'password', 'database');

$request = new SearchReplace();
$request->setDatabase($mysqli);

Database resource example shortened:

$request = new SearchReplace(new mysqli('localhost', 'user', 'password', 'database'));

Credentials example:

$request = new SearchReplace();
$request->setDatabase('localhost', 'user', 'password', 'database');
$username (string)
  • The username needed to connect to the database.
$password (string)
  • The password needed to connect to the database.
$database (string)
  • The name of the database.

search( string $needle, bool regex )

Call this method to define a search on the database. This is also useful in performing dry runs.

Parameters

$needle (string)
  • The value to find in the database
$regex (bool)
  • Whether the search needle is a RegEx pattern

replace( string $replace, bool $regex )

Call this method to define replacements on the database.

$replace (string)
  • The value to replace the search needle with
$regex (bool)
  • Whether the replace value is a RegEx pattern

includeAllTables( bool $bool = true)

Adds all tables to the search queue. Defaults to true.

Parameters

$bool (boolean)
  • True - enables adding all tables to the search queue.
  • False - disables adding all tables to the search queue.

includeTables( array $tables, bool $override = false)

Allows you to specify what tables are in the search queue.

Parameters

$tables (array)
  • An array containing the tables to perform the search on.
$override (bool)
  • Whether to override any previously set include tables.

excludeTables( array $tables )

Parameters

$tables (array)
  • An array containing the tables that should not be searched.

reset()

Resets all table inclusions, exclusions, table ranges, and table row ranges

resetTables()

Resets table inclusions and exclusions

setTableOffset( int $offset = 0)

setTableLimit( int $limit )

setTableRange()

setTableRowOffset()

setTableRowLimit()

setTableRange()

verifyPrereqs()

execute()

Executes the search and replacement.