" method."/> " method.">
Home >Backend Development >PHP Problem >How to replace php variables
php method to implement variable replacement: first create a PHP sample file; then replace the specified string through the "str_replace("world","Shanghai","Hello world!");?>" method Just drop it.
The operating environment of this article: Windows 7 system, PHP version 5.6, DELL G3 computer.
str_replace() function replaces some characters in a string with other characters (case sensitive).
This function must follow the following rules:
If the searched string is an array, then it will return an array.
If the searched string is an array, then it will find and replace each element in the array.
If you need to search and replace the array at the same time, and the elements that need to be replaced are less than the number of found elements, then the excess elements will be replaced with empty strings
If the searched is an array, and the replacement is a string, then the replacement string will work on all found values.
Note: This function is case-sensitive. Please use the str_ireplace() function to perform a case-insensitive search.
Note: This function is binary safe.
Syntax
str_replace(find,replace,string,count)
Parameters
find Required. Specifies the value to look for.
replace Required. Specifies the value to replace the value in find.
string Required. Specifies the string to be searched for.
count Optional. Variable counting the number of substitutions.
Replace the character "world" in the string "Hello world!" with "Shanghai":
<?php echo str_replace("world","Shanghai","Hello world!"); ?>
Output:
Hello Shanghai!
Recommendation: "PHP video tutorial》
The above is the detailed content of How to replace php variables. For more information, please follow other related articles on the PHP Chinese website!