| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109 |
- <?php
- /**
- * Modified version of https://github.com/interconnectit/Search-Replace-DB/blob/master/srdb.class.php (v3.1.0)
- */
- class Vamtam_Import_Search_Replace {
- /**
- * Replace all occurrences of the search string with the replacement string.
- *
- * @author Sean Murphy <sean@iamseanmurphy.com>
- * @copyright Copyright 2012 Sean Murphy. All rights reserved.
- * @license http://creativecommons.org/publicdomain/zero/1.0/
- * @link http://php.net/manual/function.str-replace.php
- *
- * @param mixed $search
- * @param mixed $replace
- * @param mixed $subject
- * @param int $count
- * @return mixed
- */
- public static function mb_str_replace( $search, $replace, $subject, &$count = 0 ) {
- if ( ! is_array( $subject ) ) {
- // Normalize $search and $replace so they are both arrays of the same length
- $searches = is_array( $search ) ? array_values( $search ) : array( $search );
- $replacements = is_array( $replace ) ? array_values( $replace ) : array( $replace );
- $replacements = array_pad( $replacements, count( $searches ), '' );
- foreach ( $searches as $key => $search ) {
- $parts = mb_split( preg_quote( $search ), $subject );
- $count += count( $parts ) - 1;
- $subject = implode( $replacements[ $key ], $parts );
- }
- } else {
- // Call mb_str_replace for each subject in array, recursively
- foreach ( $subject as $key => $value ) {
- $subject[ $key ] = self::mb_str_replace( $search, $replace, $value, $count );
- }
- }
- return $subject;
- }
- /**
- * Wrapper for regex/non regex search & replace
- *
- * @param string $search
- * @param string $replace
- * @param string $string
- * @param int $count
- *
- * @return string
- */
- public static function str_replace( $search, $replace, $string, &$count = 0 ) {
- if( function_exists( 'mb_split' ) ) {
- return self::mb_str_replace( $search, $replace, $string, $count );
- } else {
- return str_replace( $search, $replace, $string, $count );
- }
- }
- /**
- * Take a serialised array and unserialise it replacing elements as needed and
- * unserialising any subordinate arrays and performing the replace on those too.
- *
- * @param string $from String we're looking to replace.
- * @param string $to What we want it to be replaced with
- * @param array $data Used to pass any subordinate arrays back to in.
- * @param bool $serialised Does the array passed via $data need serialising.
- *
- * @return array The original array with all elements replaced as needed.
- */
- public static function recursive_unserialize_replace( $from = '', $to = '', $data = '', $serialised = false ) {
- // some unserialised data cannot be re-serialised eg. SimpleXMLElements
- try {
- if ( is_string( $data ) && ( $unserialized = @unserialize( $data ) ) !== false ) {
- $data = self::recursive_unserialize_replace( $from, $to, $unserialized, true );
- }
- elseif ( is_array( $data ) ) {
- $_tmp = array( );
- foreach ( $data as $key => $value ) {
- $_tmp[ $key ] = self::recursive_unserialize_replace( $from, $to, $value, false );
- }
- $data = $_tmp;
- unset( $_tmp );
- }
- // Submitted by Tina Matter
- elseif ( is_object( $data ) ) {
- // $data_class = get_class( $data );
- $_tmp = $data; // new $data_class( );
- $props = get_object_vars( $data );
- foreach ( $props as $key => $value ) {
- $_tmp->$key = self::recursive_unserialize_replace( $from, $to, $value, false );
- }
- $data = $_tmp;
- unset( $_tmp );
- }
- else {
- if ( is_string( $data ) ) {
- $data = self::str_replace( $from, $to, $data );
- }
- }
- if ( $serialised )
- return serialize( $data );
- } catch( Exception $error ) {
- error_log( $error->getMessage() );
- }
- return $data;
- }
- }
|