Home > Article > Backend Development > How to implement character replacement in php
php implementation method of replacing characters
php replaces characters in a string. There are usually two functions that can Use, str_replace() and strtr()
str_replace() functionDefinition and usage
str_replace() function replaces a string with other characters some characters in (case sensitive).
该函数必须遵循下列规则: 如果搜索的字符串是数组,那么它将返回数组。 如果搜索的字符串是数组,那么它将对数组中的每个元素进行查找和替换。 如果同时需要对数组进行查找和替换,并且需要执行替换的元素少于查找到的元素的数量,那么多余元素将用空字符串进行替换 如果查找的是数组,而替换的是字符串,那么替代字符串将对所有查找到的值起作用。
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必需。规定要查找的值。 replace必需。规定替换 find 中的值的值。 string必需。规定被搜索的字符串。 count可选。对替换数进行计数的变量。
strtr() function definition and usage
strtr() function converts specific strings character of.
Note: If the lengths of the from and to parameters are different, they will be formatted to the shortest length.
Grammar
strtr(string,from,to)
Or:
strtr(string,array)
Parameters
string必需。规定要转换的字符串。 from必需(除非使用数组)。规定要改变的字符。 to必需(除非使用数组)。规定要改变为的字符。 array必需(除非使用 from 和 to)。数组,其中的键名是更改的原始字符,键值是更改的目标字符。
For more related knowledge, please pay attention to PHP Chinese website! !
The above is the detailed content of How to implement character replacement in php. For more information, please follow other related articles on the PHP Chinese website!