Home > Article > Backend Development > How to replace Chinese characters in php
php method to replace Chinese characters: First use the strpos() function to find the position where the Chinese character appears in another string. If the Chinese character cannot be found, return false; then use the substr_replace() function to replace the Chinese character. Just replace the characters.
strpos() returns the position of the first occurrence of a string in another string, or FALSE if the string is not found.
(Recommended tutorial: php graphic tutorial)
Syntax:
strpos(string,find,start)
Parameters:
string Required. Specifies the string to be searched for.
#find Required. Specifies the characters to search for.
start Optional. Specifies the location from which to start the search.
substr_replace() function replaces part of a string with another string and returns the replaced string. If string is an array, the array is returned.
(Video tutorial recommendation: php video tutorial)
Grammar:
substr_replace(string,replacement,start,length)
Implementation code:
$words = ["我", "你", "他", "她"];//过滤库 $sentence = "我和你一起去他家找她";//待过滤的句子 foreach($words as $word)//遍历过滤库的词 { $len = strlen($word);//获取过滤词的长度 $pos = strpos($sentence,$word);//寻找过滤词的位置 $sentence = substr_replace($sentence,'', $pos, $len); } echo $sentence;
The above is the detailed content of How to replace Chinese characters in php. For more information, please follow other related articles on the PHP Chinese website!