Recursive string replacement within a nested array using str_replace

How can I replace a string or word within a multidimensional array with a new value using the str_replace method?

I want to update URLs across the site, but the manual process takes too much time. So, I have an idea to replace the URL in page content using the str_replace() PHP method. However, in a WordPress CMS, the page content is stored in arrays with nested structures that may contain strings or nested arrays.

For example, I have a multidimensional array, $contents, but I don't know how many dimensions it has. Since I don't know the exact structure, my question is: can I perform str_replace on all array values? Is that possible?

✅ Yes, You can define a function that performs a recursive string replacement in a nested array structure. If the value is a string, you replace the content; otherwise, you recursively call the function until a string is reached.

<?php

$current_url = 'http://buggerspot.com';
$replace_url = 'https://www.buggerspot.com';
function recursiveStrReplace($array, $search, $replace) {
  return array_map(function($item) use ($search, $replace) {
    if (is_array($item)) {
      return recursiveStrReplace($item, $search, $replace);
    } elseif (is_string($item)) {
      return str_replace($search, $replace, $item);
    }
    return $item;
  }, $array);
}

$contents = [
  'Welcome to http://buggerspot.com',
  'Visit http://buggerspot.comfor more information',
  'http://buggerspot.com offers great tools',
  'No URL here',
  [
    'Link in <a href="http://buggerspot.com">a</a> tag',
    'Another string',
    [
      'Deeply nested http://buggerspot.com',
      'No URL here either'
    ]
  ],
  'Final string without URL'
];

$contents = recursiveStrReplace($contents, $current_url, $replace_url);
print_r($contents);

recursiveStrReplace($array, $search, $replace):

  • This function takes three arguments: an array $array, a search string $search, and a replacement string $replace.
  • The function processes each item in the array. If the item is itself an array, the function is called recursively on that item.
  • If the item is a string, the str_replace() function is used to replace occurrences of $search with $replace.
  • array_map() applies this logic to each element of the array.

This approach ensures that all occurrences of the target string are replaced, regardless of their depth in the array, making it robust for complex data structures.

Real-Time Usage

In real-time applications, this code is useful when you need to perform a search and replace operation on a complex data structure, such as:

Data Migration: When migrating data between environments (e.g., from development to production), you might need to update URLs, database paths, or other strings within a nested array or object structure.

Content Management Systems (CMS): In a CMS, content may be stored in arrays with nested structures. If you decide to update URLs across the site, this function can ensure that all instances are replaced, even within nested fields.

Configuration Files: If a configuration file is represented as an array, and it contains URLs or paths that need to be updated, this function can automate the replacement.

I hope this blog will help you understand the importance of the str_replace method and how to perform a recursive function call.


Thank You!