How to perform string substitution in php
This article mainly explains "how to replace strings in php". The content in the article is simple and clear, and it is easy to learn and understand. Please follow the editor's ideas to study and learn "how to replace strings in php".
String replacement method: 1, use str_replace () function, syntax "str_replace (find value, replace value, string)"; 2, use substr_replace () function, syntax "substr_replace (string, replace value, start position, replace length)".
Operating environment of this tutorial: windows7 system, PHP7.1 version, DELL G3 computer
In PHP, you can replace specific characters or substrings in a string, which is a very common feature.
Php for string substitution
Method 1:str_ireplace () and str_replace () function
Str_ireplace () and str_replace replace the specific string specified in the original string with a new string, str_replace is case-sensitive, str_ireplace () is not case-sensitive, and the syntax is similar.
The str_replace (find,replace,string,count) str_ireplace (find,replace,string,count) parameter is required to describe find. Specifies the value to look for. Replace is required. Specifies the value that replaces the value in find. String is required. Specifies the string to be searched. Count is optional. A variable that counts the number of substitution.
Example:
Method 2:substr_replace () function
The substr_replace () function replaces part of a string with another string.
The syntax of the substr_replace () function is as follows:
The substr_replace (string,replacement,start,length) parameter is required to describe string. Specifies the string to check. Replacement is required. Specifies the string to insert. Start is required. Specifies where to start the replacement of the string.
Positive number-starts at the specified position of the string
Negative number-starts at the specified position at the end of the string
0-starts at the first character in the string
Length is optional. Specifies how many characters to replace. The default is the same length as the string.
Positive number-the length of the string being replaced
Negative number-the number of characters replaced from the end of the string
0-insert instead of replace
Example:
The execution result of the above code is:
Hi,world,hello,world thank you for reading, the above is the content of "how to replace strings in php". After the study of this article, I believe you have a deeper understanding of how to replace strings in php, and the specific use needs to be verified in practice. Here is, the editor will push for you more related knowledge points of the article, welcome to follow!