PHP array one-to-one replacement implementation code specifies that the characters between the two positions are replaced with * signs

怪我咯
Release: 2023-03-13 11:42:02
Original
1447 people have browsed it

PHP'ssubstr_replacereplaces the characters between the specified two positions with the * code. Friends in need can refer to it.

The code is as follows:

$username = "zongzi"; 
echo substr_replace($username,'**','1','2');
Copy after login

Definition and usage

substr_replace() function replaces part of the string with another string.

Syntax

substr_replace(string,replacement,start,length)
Copy after login
##charlist##Tips and
ParametersDescription
string Required. Specifies the string to check.
replacementRequired. Specifies the string to be inserted.
start

Required. Specifies where in the string to begin replacement.

  • Positive numbers - Replace starting at the startth offset

  • Negative numbers - Starting at the end of the string Start replacing

  • at offset
  • start

    0 - Start replacing

# at the first character in the string
Optional. Specifies how many characters to replace.

  • Positive number - the length of the string to be replaced

  • Negative number - the number of characters to be replaced from the end of the string

  • 0 - Insert instead of replace

Comments

Note: If

start

is negative and length is less than or equal to start, then length is 0. Example

The code is as follows:

<?php 
echo substr_replace("Hello world","earth",6); 
?>
Copy after login

Output:

Hello earth


The following method can match keywords and make special keywords respectively. The processing function, the code is as follows

<?php 
header("Content-type: text/html; charset=utf-8"); 
function multiple_replace_words($word,$replace,$string,$tmp_match=&#39;#a_a#&#39;){ 
preg_match_all(&#39;/&#39;.$word.&#39;/&#39;,$string,$matches); //匹配所有关键词 
$search = explode(&#39;,&#39;,&#39;/&#39;.implode(&#39;/,/&#39;,$matches[0]).&#39;/&#39;); 
//不存在匹配关键词 
if(empty($matches[0])) return false; 
//特殊替换设置 
$count = count($matches[0]); 
foreach($replace as $key=>$val){ 
if(!isset($matches[0][$key])) unset($replace[$key]); //剔除越界替换 
} 
//合并特殊替换数组与匹配数组 
for($i=0;$i<$count;$i++){ 
$matches[0][$i] = isset($replace[$i])? $replace[$i] : $matches[0][$i]; 
} 
$replace = $matches[0]; 
//防止替换循环,也就是替换字符仍是被替换字符,此时将其临时替换一个特定字符$tmp_match 
$replace = implode(&#39;,&#39;,$replace); 
$replace = str_replace($word,$tmp_match,$replace); //临时替换匹配字符 
$replace = explode(&#39;,&#39;,$replace); 
//替换处理 
$string = preg_replace($search,$replace,$string,1); //每次只替换数组中的一个 
$string = str_replace($tmp_match,$word,$string); //还原临时替换的匹配字符 
return $string; 
} 
//示例1 
$string = &#39;aaabaaacaaadaaa&#39;; 
$word = &#39;aaa&#39;; 
$replace = array(null,&#39;xxx&#39;,&#39;yyy&#39;); 
echo &#39;原文:&#39;.$string.&#39;<br/>输出:&#39;.multiple_replace_words($word,$replace,$string).&#39;<br/><br/>&#39;; 
//示例2 
$string = &#39;中文aaab中文ccaaad中文eee&#39;; 
$word = &#39;中文&#39;; 
$replace = array(null,&#39;(替换中文2)&#39;,&#39;(替换中文3)&#39;); 
echo &#39;原文:&#39;.$string.&#39;<br/>输出:&#39;.multiple_replace_words($word,$replace,$string); 
/* 
输出结果: 
原文:aaabaaacaaadaaa 
输出:aaabxxxcyyydaaa 
原文:中文aaab中文ccaaad中文eee 
输出:中文aaab(替换中文2)ccaaad(替换中文3)eee 
//*/
Copy after login

The above is the detailed content of PHP array one-to-one replacement implementation code specifies that the characters between the two positions are replaced with * signs. 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
Popular Tutorials
More>
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!