Home  >  Article  >  Backend Development  >  How to replace part of the content in php

How to replace part of the content in php

藏色散人
藏色散人Original
2022-11-25 09:27:431588browse

php method to replace part of the content: 1. Create a php sample file; 2. Calculate the string length through "mb_strlen($username,'utf-8');"; 3. Intercept the first part according to the length String; 4. Intercept the last part of the string or remove the first two parts of the string; 5. Replace characters through "str_replace_once".

How to replace part of the content in php

The operating environment of this tutorial: Windows 7 system, PHP version 8.1, Dell G3 computer.

How to replace part of the content in php?

php Chinese string middle part character replacement operation

1. Steps: 1. Calculate the length

2. Intercept the first part of the string according to the length

3. Intercept the middle part of the string

4. Intercept the last part of the string or remove the first two parts of the string

5. Replace the middle part of the string with an asterisk

6. Connection string

2. Code writing:

/**
* 中文字符串中间部分替换(最多替换二分之一)
* @param string $username 中文字符串
* @return string 处理后的字符串
*/
function ceshi($username){
    // 计算字符串长度,无论汉字还是英文字符全部为1
    $length = mb_strlen($username,'utf-8');
    
    // 截取第一部分代码
    $firstStr1 = mb_substr($username,0,ceil($length/4),'utf-8');
    // 截取中间部分代码
    $firstStr = mb_substr($username,ceil($length/4),floor($length/2),'utf-8');
    // (方法一)截取剩余字符串
    $firstStr2 = mb_substr($username,ceil($length/4) + floor($length/2), floor(($length+1)/2 - 1),'utf-8');
    // (方法二)是直接去掉前两部分字符串
    // $firstStr2 = "";
    // if($firstStr && $username){
        // $firstStr2 = str_replace_once($username,$firstStr1,"");
        // $firstStr2 = str_replace_once($firstStr2,$firstStr,"");
    // }
    
    return $firstStr1 . str_repeat("*",mb_strlen($firstStr,'utf-8')) . $firstStr2;
}
 
 
/**
* 替换字符串中的某些字符,仅替换一次,且替换内容必须在原始字符串内
* @param string $username 字符串
* @param string $find 替换内容
* @param string $replace 目标内容
* @param string 处理后的额字符串
*/
function str_replace_once($username,$find,$replace)
{   
    // 查询出字符串第一次出现的位置
    $pos = mb_strpos($username,$find);
    // 替换目标内的信息
    return substr_replace($username, $replace, $pos, strlen($find));
}

3. Function analysis

1. mb_strlen()

Function: return character The length of the string. Advantages: You can set the character encoding to return the corresponding number of characters, which solves the length problem of Chinese strings well.

Syntax: strlen(string $str, string $encoding)

str: ​​string content

encoding: encoding format For example: gbk/utf8/gb2312/leave blank Different The encoding format has different lengths for Chinese, and the default is utf8

Code example:

$str='33三三three';
echo strlen($str).&#39;<br>&#39;;  //13   数字+中文+英文=2+6+5
echo mb_strlen($str,&#39;utf8&#39;).&#39;<br>&#39;;//9 //  数字+中文+英文=2+2+5
echo mb_strlen($str,&#39;gbk&#39;).&#39;<br>&#39;;//10   数字+中文+英文=2+3+5
echo mb_strlen($str,&#39;gb2312&#39;).&#39;<br>&#39;;//11     数字+中文+英文=2+4+5

2, mb_substr()

Function: Return part of the string, advantages : Suitable for segmentation of Chinese characters

Syntax: mb_substr (string $str, int $start, int $length, string $encoding)

str: ​​String content

start: starting position Positive number - starts at the specified position of the string; negative number - starts at the specified position from the end of the string; 0 - starts at the first character in the string

length: length Positive Number - returned from the position of the start parameter Negative number - returned from the end of the string

encoding: encoding For example: gbk/utf8/gb2312/leave blank Different encoding formats have different lengths for Chinese, generally combined with mb_strlen Use utf8

Note: 1). If the start parameter is a negative number and length is less than or equal to start, the length is 0

2). Generally speaking, just use utf8

Code example

$str=&#39;33三三three&#39;;
echo mb_substr($str,0,3,&#39;utf8&#39;).&#39;<br>&#39;;  // 33三三

3. str_repeat()

Function: Repeat the string a specified number of times

Syntax: str_repeat(string $str,int $repeat)

str:String

repeat:Number of repetitions

Code example

$str=&#39;33三三&#39;;
echo str_repeat($str,10).&#39;<br>&#39;;  // 33三三33三三33三三33三三33三三33三三33三三33三三33三三33三三

4, mb_stropos()

Function: Find the string in The position of the first occurrence in another string (not case sensitive)

Syntax: mb_stropos(string $str,string $find,int $start,string $encoding)

str : Original string

find: Query content

start: Start search position, the default is 0

encoding: Encoding format Default utf8 Normally use utf8

Code example

$str=&#39;33三二一&#39;;
echo mb_strpos($str,"一",0).&#39;<br>&#39;;  //  4
echo mb_strpos($str,"一",0,&#39;utf8&#39;).&#39;<br>&#39;;//  4
echo mb_strpos($str,"一",0,&#39;gbk&#39;).&#39;<br>&#39;;//   5

5. substr_replace()

Function: Replace part of a string with another string

Syntax: substr_replace(string $str,string $replacement,int $start,int $length)

str: ​​string information

replacement: string to be replaced

start: starting position

length: length

Code example

echo substr_replace("天气晚来秋","沉",3,3);  // 天沉晚来秋

Note: When replacing Chinese characters, you need to pay attention to the three bytes of a Chinese character

Recommended learning: "PHP Video Tutorial

The above is the detailed content of How to replace part of the content in php. For more information, please follow other related articles on the PHP Chinese website!

Statement:
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