How to replace substring of string in PHP

PHPz
Release: 2024-03-19 10:12:01
forward
406 people have browsed it

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

phpprovides several built-in functions to replace substrings in strings, includingstr_replace(),preg_replace()andstrtr ().

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])
Copy after login

$searchis the substring to be searched,$replaceis the replacement substring,$subjectis 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!
Copy after login

preg_replace()

preg_replace()Function usesregular expressionto replace a substring in a string. Its syntax is:

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

$patternis a regular expression used to match substrings,$replacementis the replacement substring,$subjectis 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.
Copy after login

strtr()

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

string strtr(string $str, string $from, string $to)
Copy after login

$stris the string to be replaced,$fromis the character to be found, and$tois the replacement character.

Example:

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

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 thestr_ireplace()(case-insensitive) andpreg_replace_callback()(custom replacement) functions.
  • You can also use string interpolation and thesubstr_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!

source:lsjlt.com
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template
About us Disclaimer Sitemap
php.cn:Public welfare online PHP training,Help PHP learners grow quickly!