3 replacement methods: 1. Use substr_replace() to replace all characters starting from the head of the string, the syntax is "substr_replace (original string, specify replacement value, 0)". 2. Use str_replace() to replace all characters, the syntax is "str_replace(original string, specified replacement value, original string)". 3. Use str_ireplace() to replace all characters, the syntax is "str_ireplace(original string, specified replacement value, original string)".
The operating environment of this tutorial: windows7 system, PHP8.1 version, DELL G3 computer
Method 1: Use substr_replace() function
substr_replace() function replaces part of a string with another string.
substr_replace(string,replacement,start,length)
Copy after login
substr_replace() Replaces the substring qualified by the start and optional length parameters with replacement in a copy of string string.
If start is a positive number, replacement will start from the start position of string. If start is negative, the replacement will start at the start position from the bottom of string.
If the length parameter is set and is a positive number, it represents the length of the replaced substring in string. If set to a negative number, it represents the number of characters from the end of the substring to be replaced from the end of string. If this parameter is not provided, the default is strlen(string) (the length of the string). Of course, if length is 0, then the function of this function is to insert replacement at the start position of string.
Parameters
Description
string
Required. Specifies the string to check.
replacement
Required. Specifies the string to be inserted.
start
Required. Specifies where in the string to begin replacement.
Positive number - starts at the specified position in the string
Negative number - starts at the specified position from the end of the string
0 - at the first character in the string Start at
length
Optional. Specifies how many characters to replace. The default is the same as the string length.
Positive number - the length of the string to be replaced
Negative number - the number of characters to be replaced from the end of the string
0 - insert instead of replace
Example: Replace all characters in a string
Just set the third parameter of the function to 0, the third Set each parameter to the length of the original string or omit it to replace all characters
##Method 2/Method 3: str_ireplace() and str_replace() Function
str_ireplace() and str_replace both use a new string to replace the specified substring in the original string. If the substring to be replaced is the original string, you can replace the original string. All characters in the string. The syntax of str_ireplace() and str_replace is similar, the difference is that str_replace is case-sensitive, str_ireplace() is not case-sensitive
The above is the detailed content of How to replace all characters in a string in php. For more information, please follow other related articles on the PHP Chinese website!
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
PHP 8.4 brings several new features, security improvements, and performance improvements with healthy amounts of feature deprecations and removals.
This guide explains how to install PHP 8.4 or upgrade to PHP 8.4 on Ubuntu, Debian, or their derivati
CakePHP is an open-source framework for PHP. It is intended to make developing, deploying and maintaining applications much easier. CakePHP is based on a MVC-like architecture that is both powerful and easy to grasp. Models, Views, and Controllers gu
Visual Studio Code, also known as VS Code, is a free source code editor — or integrated development environment (IDE) — available for all major operating systems.
With a large collection of extensions for many programming languages, VS Code can be c