search
HomeBackend DevelopmentPHP ProblemWhat are the find and replace functions in php

Replacement functions: 1. str_ireplace(); 2. str_replace(); 3. substr_replace(); 4. array_replace(); 5. array_replace_recursive(); 6. array_splice().

What are the find and replace functions in php

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 = &#39;hello,world,hello,world&#39;;
$replace = &#39;hi&#39;;
$search = &#39;hello&#39;;
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 = &#39;hello,world,hello,world&#39;;
$replace = &#39;hi&#39;;
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!

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
ACID vs BASE Database: Differences and when to use each.ACID vs BASE Database: Differences and when to use each.Mar 26, 2025 pm 04:19 PM

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

PHP Secure File Uploads: Preventing file-related vulnerabilities.PHP Secure File Uploads: Preventing file-related vulnerabilities.Mar 26, 2025 pm 04:18 PM

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.

PHP Input Validation: Best practices.PHP Input Validation: Best practices.Mar 26, 2025 pm 04:17 PM

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.

PHP API Rate Limiting: Implementation strategies.PHP API Rate Limiting: Implementation strategies.Mar 26, 2025 pm 04:16 PM

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

PHP Password Hashing: password_hash and password_verify.PHP Password Hashing: password_hash and password_verify.Mar 26, 2025 pm 04:15 PM

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

OWASP Top 10 PHP: Describe and mitigate common vulnerabilities.OWASP Top 10 PHP: Describe and mitigate common vulnerabilities.Mar 26, 2025 pm 04:13 PM

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.

PHP XSS Prevention: How to protect against XSS.PHP XSS Prevention: How to protect against XSS.Mar 26, 2025 pm 04:12 PM

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

PHP Interface vs Abstract Class: When to use each.PHP Interface vs Abstract Class: When to use each.Mar 26, 2025 pm 04:11 PM

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

See all articles

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)
3 weeks agoBy尊渡假赌尊渡假赌尊渡假赌
R.E.P.O. Best Graphic Settings
3 weeks agoBy尊渡假赌尊渡假赌尊渡假赌
R.E.P.O. How to Fix Audio if You Can't Hear Anyone
3 weeks agoBy尊渡假赌尊渡假赌尊渡假赌
WWE 2K25: How To Unlock Everything In MyRise
4 weeks agoBy尊渡假赌尊渡假赌尊渡假赌

Hot Tools

ZendStudio 13.5.1 Mac

ZendStudio 13.5.1 Mac

Powerful PHP integrated development environment

SublimeText3 English version

SublimeText3 English version

Recommended: Win version, supports code prompts!

DVWA

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

SublimeText3 Chinese version

Chinese version, very easy to use

EditPlus Chinese cracked version

EditPlus Chinese cracked version

Small size, syntax highlighting, does not support code prompt function