Home > Backend Development > PHP Problem > How to replace all characters in a string in php

How to replace all characters in a string in php

青灯夜游
Release: 2023-03-16 22:40:01
Original
20259 people have browsed it

3 replacement methods: 1. Use substr_replace() to replace all characters starting from the head of the string, the syntax is "substr_replace (original string, specify replacement value, 0)". 2. Use str_replace() to replace all characters, the syntax is "str_replace(original string, specified replacement value, original string)". 3. Use str_ireplace() to replace all characters, the syntax is "str_ireplace(original string, specified replacement value, original string)".

How to replace all characters in a string in php

The operating environment of this tutorial: windows7 system, PHP8.1 version, DELL G3 computer

Method 1: Use substr_replace() function

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

substr_replace(string,replacement,start,length)
Copy after login
  • substr_replace() Replaces the substring qualified by the start and optional length parameters with replacement in a copy of string string.

  • If start is a positive number, replacement will start from the start position of string. If start is negative, the replacement will start at the start position from the bottom of string.

  • If the length parameter is set and is a positive number, it represents the length of the replaced substring in string. If set to a negative number, it represents the number of characters from the end of the substring to be replaced from the end of string. If this parameter is not provided, the default is strlen(string) (the length of the string). Of course, if length is 0, then the function of this function is to insert replacement at the start position of string.

ParametersDescription
stringRequired. Specifies the string to check.
replacementRequired. Specifies the string to be inserted.
startRequired. Specifies where in the string to begin replacement.
  • Positive number - starts at the specified position in the string
  • Negative number - starts at the specified position from the end of the string
  • 0 - at the first character in the string Start at
length Optional. Specifies how many characters to replace. The default is the same as the string length.
  • 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

Example: Replace all characters in a string

Just set the third parameter of the function to 0, the third Set each parameter to the length of the original string or omit it to replace all characters

<?php
header(&#39;content-type:text/html;charset=utf-8&#39;);   
$str = &#39;hello world!&#39;;
echo "原字符串:".$str."<br><br>";
$replace = &#39;ABCDEFGHIJKL&#39;;
echo "替换全部字符字符:".substr_replace($str, $replace,0)."<br>";
?>
Copy after login

How to replace all characters in a string in php

##Method 2/Method 3: str_ireplace() and str_replace() Function

str_ireplace() and str_replace both use a new string to replace the specified substring in the original string. If the substring to be replaced is the original string, you can replace the original string. All characters in the string.

The syntax of str_ireplace() and str_replace is similar, the difference is that str_replace is case-sensitive, str_ireplace() is not case-sensitive

str_replace(find,replace,string,count)
str_ireplace(find,replace,string,count)
Copy after login

ParametersDescriptionRequired. Specifies the value to look for. Required. Specifies a value that replaces the value in Required. Specifies the string to be searched for. Optional. Variable counting the number of substitutions.

示例:替换字符串中的所有字符

只需要将第一个参数设置为原字符串值即可。

<?php
header(&#39;content-type:text/html;charset=utf-8&#39;);   
$str = &#39;hello world!&#39;;
echo "原字符串:".$str."<br><br>";
$replace = &#39;ABCDEFGHIJKL&#39;;
echo "替换全部字符字符:".str_replace($str, $replace, $str)."<br>";
echo "替换全部字符字符:".str_ireplace($str, $replace, $str)."<br>";
?>
Copy after login

How to replace all characters in a string in php

扩展知识:替换字符串还可利用正则替换函数preg_replace() 和preg_filter()

preg_replace() 和preg_filter()函数都可以执行正则表达式的搜索和替换,不同的是 preg_filter() 函数只返回匹配成功的结果,而 preg_replace() 返回所有结果,不管是否匹配成功。

preg_replace() 和preg_filter()函数的语法类似:

preg_replace($pattern, $replacement, $subject [, $limit = -1 [, &$count]])
preg_filter($pattern, $replacement, $subject [, $limit = -1 [, &$count]])
Copy after login

搜索 $subject 中匹配 $pattern 的部分, 以 $replacement 进行替换。

参数说明如下:

  • $pattern:要搜索的模式,可以使一个字符串或字符串数组;

  • $replacement:用于替换的字符串或字符串数组。如果这个参数是一个字符串,并且 $pattern 是一个数组,那么所有的模式都使用这个字符串进行替换。如果 $pattern 和 $replacement 都是数组,每个 $pattern 使用 $replacement 中对应的元素进行替换。如果 $replacement 中的元素比 $pattern 中的少,多出来的 $pattern 使用空字符串进行替换。

  • $subject:要进行搜索和替换的字符串或字符串数组,如果 $subject 是一个数组,搜索和替换回在 $subject 的每一个元素上进行, 并且返回值也会是一个数组。

  • $limit:可选参数,每个模式在每个 $subject 上进行替换的最大次数。默认是 -1(无限)。

  • $count:可选参数,如果指定,将会被填充为完成的替换次数。

示例:

preg_filter()和preg_replace()利用正则来替换字符串

<?php
header(&#39;content-type:text/html;charset=utf-8&#39;);
$subject = array(&#39;1&#39;, &#39;a&#39;, &#39;2&#39;, &#39;b&#39;, &#39;3&#39;, &#39;A&#39;, &#39;B&#39;, &#39;4&#39;); 
$pattern = array(&#39;/\d/&#39;, &#39;/[a-z]/&#39;, &#39;/[1a]/&#39;); 
$replace = array(&#39;A:$0&#39;, &#39;B:$0&#39;, &#39;C:$0&#39;); 
 
echo "preg_filter 返回值:\n";
var_dump(preg_filter($pattern, $replace, $subject)); 
 
echo "preg_replace 返回值:\n";
var_dump(preg_replace($pattern, $replace, $subject)); 
?>
Copy after login

How to replace all characters in a string in php

推荐学习:《PHP视频教程

The above is the detailed content of How to replace all characters in a string 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
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template
find
replacefind.
string
count