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

Oct 14, 2022 pm 07:08 PM
php php string

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!

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

Hot AI Tools

Undresser.AI Undress

Undresser.AI Undress

AI-powered app for creating realistic nude photos

AI Clothes Remover

AI Clothes Remover

Online AI tool for removing clothes from photos.

Undress AI Tool

Undress AI Tool

Undress images for free

Clothoff.io

Clothoff.io

AI clothes remover

AI Hentai Generator

AI Hentai Generator

Generate AI Hentai for free.

Hot Article

R.E.P.O. Energy Crystals Explained and What They Do (Yellow Crystal)
2 weeks ago By 尊渡假赌尊渡假赌尊渡假赌
Repo: How To Revive Teammates
1 months ago By 尊渡假赌尊渡假赌尊渡假赌
Hello Kitty Island Adventure: How To Get Giant Seeds
4 weeks ago By 尊渡假赌尊渡假赌尊渡假赌

Hot Tools

Notepad++7.3.1

Notepad++7.3.1

Easy-to-use and free code editor

SublimeText3 Chinese version

SublimeText3 Chinese version

Chinese version, very easy to use

Zend Studio 13.0.1

Zend Studio 13.0.1

Powerful PHP integrated development environment

Dreamweaver CS6

Dreamweaver CS6

Visual web development tools

SublimeText3 Mac version

SublimeText3 Mac version

God-level code editing software (SublimeText3)

CakePHP Project Configuration CakePHP Project Configuration Sep 10, 2024 pm 05:25 PM

In this chapter, we will understand the Environment Variables, General Configuration, Database Configuration and Email Configuration in CakePHP.

PHP 8.4 Installation and Upgrade guide for Ubuntu and Debian PHP 8.4 Installation and Upgrade guide for Ubuntu and Debian Dec 24, 2024 pm 04:42 PM

PHP 8.4 brings several new features, security improvements, and performance improvements with healthy amounts of feature deprecations and removals. This guide explains how to install PHP 8.4 or upgrade to PHP 8.4 on Ubuntu, Debian, or their derivati

CakePHP Date and Time CakePHP Date and Time Sep 10, 2024 pm 05:27 PM

To work with date and time in cakephp4, we are going to make use of the available FrozenTime class.

CakePHP File upload CakePHP File upload Sep 10, 2024 pm 05:27 PM

To work on file upload we are going to use the form helper. Here, is an example for file upload.

CakePHP Routing CakePHP Routing Sep 10, 2024 pm 05:25 PM

In this chapter, we are going to learn the following topics related to routing ?

Discuss CakePHP Discuss CakePHP Sep 10, 2024 pm 05:28 PM

CakePHP is an open-source framework for PHP. It is intended to make developing, deploying and maintaining applications much easier. CakePHP is based on a MVC-like architecture that is both powerful and easy to grasp. Models, Views, and Controllers gu

How To Set Up Visual Studio Code (VS Code) for PHP Development How To Set Up Visual Studio Code (VS Code) for PHP Development Dec 20, 2024 am 11:31 AM

Visual Studio Code, also known as VS Code, is a free source code editor — or integrated development environment (IDE) — available for all major operating systems. With a large collection of extensions for many programming languages, VS Code can be c

CakePHP Creating Validators CakePHP Creating Validators Sep 10, 2024 pm 05:26 PM

Validator can be created by adding the following two lines in the controller.

See all articles
find
replacefind.
string
count