Replacing String with Substring in PHP

To replace a string with a substring in PHP, use the str_replace($search, $replace, $subject, $count) function. The str_replace() function takes the string to search for as the first argument, the string to replace the old value with as the second argument, and the string where the replacement is to take place as the third argument. The str_replace() function does not modify the original string; instead, it returns a copy of the replaced string. If the optional argument $count is passed, it will contain the number of replacements performed. You can pass arrays to the $search, and $replace to perform multiple replacements simultaneously. You can also replace strings with PHP Regular Expressions (see example below). In this PHP string replacement example, we replace all occurrences of the search string with a substring using the str_replace() function. Click Execute to run the PHP String Replace Example online and see the result.
Replacing String with Substring in PHP Execute
<?php
  echo str_replace("hard", "easy", "Replacing a string in PHP is hard." );
?>
Updated: Viewed: 3349 times

What is a string in PHP?

A PHP string is a sequence of characters in which a character corresponds to a byte. PHP strings cannot support Unicode strings because a byte can only represent 256 characters. To create a string in PHP, you can use: single quotes ('...'), double-quoted strings ("..."), and Heredoc syntax (<<<). PHP provides many built-in functions for interpolating, replacing, splitting, concatenating, and comparing strings. You can also convert string to lowercase, convert string to int, convert string to array and find the length of a string.

How to replace a string with a substring in PHP?

To replace a string with a substring in PHP, you can use the str_replace() function. The following is the syntax of the str_replace() function:

PHP str_replace() Syntax
str_replace($search, $replace, $subject, $count = null)

Where:
  • $search (required): the string to search for;
  • $replace (required): the string to replace with;
  • $subject (required): the string or an array of strings where we want to replace;
  • $count (optional): the number of replacements to perform;
PHP str_replace() Example
<?php
echo str_replace("Earth", "World", "Hello, Earth!" );
?>

#output: Hello, World!

PHP String Replace Examples

The following are examples of replacing strings in PHP:

Replace all occurrences of a string

The following is an example of replacing all occurrences of a string in a PHP string:

Replace All Occurrences in PHP String Example
<?php
echo str_replace("fine", "good", "I'm fine, I'm fine, I'm fine");
?>

#output: I'm good, I'm good, I'm good

Removing a substring in a string

To remove a substring from a string, you need to pass an empty string ("...") in the $replace argument. The following is an example of removing a substring in a PHP string:

Removing a substring using str_replace()
<?php
echo str_replace("Replace", "", "PHP String Replace");
?>

#output: PHP String

How to replace a PHP string with Regular Expression?

To replace a PHP string using a Regular Expression, you can use the preg_replace() function. The main difference between preg_replace() and str_replace() functions is that preg_replace() will perform Regular Expression pattern matching, while str_replace() will replace a specific string with another string, and it will be much faster and more efficient. The following is the syntax of the preg_replace() function:

PHP preg_replace() Syntax
preg_replace($pattern, $replacement, $subject, $limit, $count)

Where:
  • $pattern (required): a pattern containing a Regular Expression to search for content;
  • $replacement (required): the string to replace;
  • $subject (required): string to perform the replacement;
  • $limit (optional): specifies the maximum possible number of replacements for each pattern. The default is -1, which means unlimited;
  • $count (optional): the variable will contain the number of replacements performed after the function is executed.
PHP preg_replace() Example
<?php
echo preg_replace('/Contains/', 'Replace', 'PHP String Contains Example');
?>

#output: PHP String Replace Example

Replacing PHP String with Regular Expressions Examples

The following are examples of replacing a PHP string using Regular Expressions in PHP:

Using the Regular Expression to replace the first occurrence of a string

The following is an example of replacing the first occurrence in a PHP string using Regular Expressions:

Replace First Occurrence using preg_replace()
<?php
echo preg_replace("/fine/", "good", "I'm fine, I'm fine, I'm fine", 1);
?>

#output: I'm good, I'm fine, I'm fine

Using Regular Expressions to replace string, case-insensitively:

To replace a case-insensitive string with Regex, you need to add an "i" anchor to the Regular Expression:

PHP preg_replace() Case Insensitive Example
<?php
echo preg_replace('/contains/i', 'Replace', 'PHP String Contains Example');
?>

#output: PHP String Replace Example 

Using the Regular Expressions function to remove all spaces

The following is an example of removing all spaces in a PHP string using Regular Expressions:

Removing all spaces using preg_replace()
<?php
echo preg_replace('/\s+/', '', 'PHP       String     Contains');
?>

#output: PHPStringContains

See also