Home  >  Article  >  Backend Development  >  How to replace substring of string in PHP

How to replace substring of string in PHP

PHPz
PHPzforward
2024-03-19 10:10:29440browse

php editor Apple teaches you how to replace substrings of strings. In PHP, simple string replacement operations can be achieved using the str_replace() function. This function accepts three parameters: the substring to be replaced, the replaced string, and the original string. By calling this function, you can quickly replace the specified substring in the string and easily modify and update the string content. Below we will introduce in detail how to use the str_replace() function in PHP to perform substring replacement of strings.

PHP Replace string substring

php provides several built-in functions to replace substrings in strings, including str_replace(), preg_replace() and strtr ().

str_replace()

str_replace() The function replaces all instances in a string that match the specified substring with a new substring. Its syntax is:

string str_replace(string $search, string $replace, string $subject [, int $count])

$search is the substring to be searched, $replace is the replacement substring, $subject is the string to be replaced, $count (optional) is the maximum number of substitutions.

Example:

$str = "Hello, world!";
$newStr = str_replace("world", "universe", $str);
// Output: Hello, universe!

preg_replace()

preg_replace() Function uses regular expression to replace a substring in a string. Its syntax is:

string preg_replace(string $pattern, string $replacement, string $subject [, int $limit, int &$count])

$pattern is a regular expression used to match substrings, $replacement is the replacement substring, $subject is the character to be replaced String, $limit (optional) is the maximum number of substitutions, $count (optional) is a reference to the number of matches.

Example:

$str = "The quick brown fox jumps over the lazy dog.";
$newStr = preg_replace("/the/i", "The", $str);
// Output: The Quick brown fox jumps over The lazy dog.

strtr()

strtr() Function replaces specific characters in a string with specified characters. Its syntax is:

string strtr(string $str, string $from, string $to)

$str is the string to be replaced, $from is the character to be found, and $to is the replacement character.

Example:

$str = "Hello, world!";
$newStr = strtr($str, "!,", ".");
// Output: Hello. world.

Performance comparison

The performance of these three functions varies depending on the use case. For simple replacement, str_replace() is usually fastest. If you need to use regular expressions, preg_replace() is the best choice. For character mapping, strtr() is the fastest and most efficient.

in addition

  • Other advanced replacements can be made using the str_ireplace() (case-insensitive) and preg_replace_callback() (custom replacement) functions.
  • You can also use string interpolation and the substr_replace() function to replace substrings of a string.
  • It is important to use escape characters correctly to prevent injection attacks.

The above is the detailed content of How to replace substring of string in PHP. For more information, please follow other related articles on the PHP Chinese website!

Statement:
This article is reproduced at:lsjlt.com. If there is any infringement, please contact admin@php.cn delete