How to replace Chinese characters in php

王林
Release: 2023-03-04 14:48:02
Original
3717 people have browsed it

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.

How to replace Chinese characters in php

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)
Copy after login

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)
Copy after login

Implementation code:

$words = ["我", "你", "他", "她"];//过滤库 $sentence = "我和你一起去他家找她";//待过滤的句子 foreach($words as $word)//遍历过滤库的词 { $len = strlen($word);//获取过滤词的长度 $pos = strpos($sentence,$word);//寻找过滤词的位置 $sentence = substr_replace($sentence,'', $pos, $len); } echo $sentence;
Copy after login

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!

Related labels:
source:php.cn
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
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template
About us Disclaimer Sitemap
php.cn:Public welfare online PHP training,Help PHP learners grow quickly!