Replacement functions: 1. str_ireplace(); 2. str_replace(); 3. substr_replace(); 4. array_replace(); 5. array_replace_recursive(); 6. array_splice().
The operating environment of this tutorial: windows7 system, PHP7.1 version, DELL G3 computer
php string search Replacement function
str_ireplace(): Replace some characters in the string (not case sensitive).
str_replace(): Replace some characters in the string (case-sensitive).
substr_replace(): Replace part of a string with another string.
str_ireplace() and str_replace() functions
str_ireplace() and str_replace use a new string to replace the specified string in the original string. For strings, str_replace is case-sensitive, str_ireplace() is not case-sensitive, and their syntax is similar.
The syntax of str_ireplace() is as follows:
mixed str_ireplace ( mixed $search , mixed $replace , mixed $subject [, int &$count ] )
This function returns a string or array. This string or array is the result of replacing all search in subject with replace (ignoring case). The parameter count represents the number of times to perform the replacement.
Usage examples are as follows:
<?php $str = 'hello,world,hello,world'; $replace = 'hi'; $search = 'hello'; echo str_ireplace($search, $replace, $str); ?>
The output result of executing the above code is:
hi,world,hi,world
substr_replace() function
substr_replace( ) The syntax of the function is as follows:
mixed substr_replace ( mixed $string , mixed $replacement , mixed $start [, mixed $length ] )
substr_replace() Replaces the substring qualified by the start and optional length parameters in a copy of string string using replacement.
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.
The usage example of this function is as follows:
<?php $str = 'hello,world,hello,world'; $replace = 'hi'; echo substr_replace($str, $replace, 0,5); ?>
The execution result of the above code is:
hi,world,hello,world
php array search and replacement function
array_replace(): Replace the value of the first array with the value of the subsequent array.
array_replace_recursive(): Recursively replace the value of the first array with the value of the subsequent array.
array_splice(): Remove and replace the specified elements in the array.
array_splice() function
array_splice() function is used to delete part of the elements of the array; you can delete it directly or use other values. to replace.
array_splice() syntax is as follows:
array array_splice ( array &$arr, int $start [, int $length = 0 [, mixed $replacement ]] )
Parameter description:
- arr represents an array.
- start indicates the position (subscript) where deletion begins:
- If start is a positive number, delete from front to back.
- If start is a negative number, start from the position -start from the end of arr and delete from back to front. For example -2 means start from the second to last element of the array.
- length is an optional parameter, indicating the number of elements to be deleted:
- If length is a positive number, it means length elements are deleted;
- If length is a negative number, all elements starting from start and counting down to length from the end of the array will be deleted;
- If it is omitted, all elements starting from start and ending at the end of the array will be deleted.
- replacement is an optional parameter indicating the value to be replaced. If replacement has multiple values, it needs to be set to an array. If there is only one value, it does not need to be set to an array.
If the result of the combination of start and length is that no elements will be deleted, then the value contained in replacement will be inserted into the position specified by start.
Note that using replacement to replace array elements will not retain the original key names.
Return value: Returns an array consisting of the deleted elements.
Examples of using the function are as follows:
<?php $arr = array("red", "green", "blue", "yellow"); array_splice($arr, 2); print_r($arr); //$arr 现在变成 array("red", "green") $arr = array("red", "green", "blue", "yellow"); array_splice($arr, 1, -1); print_r($arr); //$arr 现在变成 array("red", "yellow") $arr = array("red", "green", "blue", "yellow"); array_splice($arr, 1, count($arr), "orange"); print_r($arr); //$arr 现在变成 array("red", "orange") $arr = array("red", "green", "blue", "yellow"); array_splice($arr, -1, 1, array("black", "maroon")); print_r($arr); //$input 现在变成 array("red", "green", "blue", "black", "maroon") $arr = array("red", "green", "blue", "yellow"); array_splice($arr, 3, 0, "purple"); print_r($arr); //$arr 现在变成 array("red", "green", "blue", "purple", "yellow"); ?>
The output result of executing the above program is as follows:
Array ( [0] => red [1] => green ) Array ( [0] => red [1] => yellow ) Array ( [0] => red [1] => orange ) Array ( [0] => red [1] => green [2] => blue [3] => black [4] => maroon ) Array ( [0] => red [1] => green [2] => blue [3] => purple [4] => yellow )
Recommended learning: "PHP Video Tutorial"
The above is the detailed content of What are the find and replace functions in php. For more information, please follow other related articles on the PHP Chinese website!

The article compares ACID and BASE database models, detailing their characteristics and appropriate use cases. ACID prioritizes data integrity and consistency, suitable for financial and e-commerce applications, while BASE focuses on availability and

The article discusses securing PHP file uploads to prevent vulnerabilities like code injection. It focuses on file type validation, secure storage, and error handling to enhance application security.

Article discusses best practices for PHP input validation to enhance security, focusing on techniques like using built-in functions, whitelist approach, and server-side validation.

The article discusses strategies for implementing API rate limiting in PHP, including algorithms like Token Bucket and Leaky Bucket, and using libraries like symfony/rate-limiter. It also covers monitoring, dynamically adjusting rate limits, and hand

The article discusses the benefits of using password_hash and password_verify in PHP for securing passwords. The main argument is that these functions enhance password protection through automatic salt generation, strong hashing algorithms, and secur

The article discusses OWASP Top 10 vulnerabilities in PHP and mitigation strategies. Key issues include injection, broken authentication, and XSS, with recommended tools for monitoring and securing PHP applications.

The article discusses strategies to prevent XSS attacks in PHP, focusing on input sanitization, output encoding, and using security-enhancing libraries and frameworks.

The article discusses the use of interfaces and abstract classes in PHP, focusing on when to use each. Interfaces define a contract without implementation, suitable for unrelated classes and multiple inheritance. Abstract classes provide common funct


Hot AI Tools

Undresser.AI Undress
AI-powered app for creating realistic nude photos

AI Clothes Remover
Online AI tool for removing clothes from photos.

Undress AI Tool
Undress images for free

Clothoff.io
AI clothes remover

AI Hentai Generator
Generate AI Hentai for free.

Hot Article

Hot Tools

ZendStudio 13.5.1 Mac
Powerful PHP integrated development environment

SublimeText3 English version
Recommended: Win version, supports code prompts!

DVWA
Damn Vulnerable Web App (DVWA) is a PHP/MySQL web application that is very vulnerable. Its main goals are to be an aid for security professionals to test their skills and tools in a legal environment, to help web developers better understand the process of securing web applications, and to help teachers/students teach/learn in a classroom environment Web application security. The goal of DVWA is to practice some of the most common web vulnerabilities through a simple and straightforward interface, with varying degrees of difficulty. Please note that this software

SublimeText3 Chinese version
Chinese version, very easy to use

EditPlus Chinese cracked version
Small size, syntax highlighting, does not support code prompt function