php implementation method of finding and replacing strings: 1. Use the "substr_replace()" function to replace part of the string with another string; 2. Use the "str_replace()" function to replace a string other characters in the string.
Recommended: "PHP Video Tutorial"
PHP String Replacement
Used to replace the specified string from a string.
Relevant functions are as follows:
substr_replace(): Replace part of a string with another string
str_replace(): Use a string to replace a part of a string Other characters
substr_replace()
substr_replace() function is used to replace part of a string with another string and return a mixed type.
Syntax:
mix substr_replace ( mixed string, string replacement, int start [, int length] )
Parameter description is as follows:
Parameter Description
string The string to be processed
replacement The character to be inserted String
start The starting position of the string. The starting position is 0. If it is negative, it starts from the specified position at the end of the string.
length is optional. The length returned by the string. The default is until characters The end of the string, if it is negative, return from the end of the string
Example:
<?php echo substr_replace('abcdef', '###', 1);//输出 a### echo substr_replace('abcdef', '###', 1, 2);//输出 a###def echo substr_replace('abcdef', '###', -3, 2);//输出 abc###f echo substr_replace('abcdef', '###', 1, -2);//输出 a###ef ?>
Hint
If start is a negative number and length is less than or equal to start, length is 0.
str_replace()
str_replace() function uses one string to replace other characters in the string and returns a mixed type.
Syntax:
mixed str_replace( mixed search, mixed replace, mixed string [, int &count] )
The parameter description is as follows:
Parameter Description
search The string to be found (replaced)
replace The string to replace search
string The string to process
count Optional, a variable that counts the replacement
Example:
<?php echo str_replace("world","earth","Hello world!");//输出 Hello earth! //替换多个,且第二个参数为空字符 echo str_replace("o","","Hello world!");//输出 Hell wrld! //使用数组 $arr = array("e", "o"); $arr2 = array("x", "y"); echo str_replace($arr, $arr2, "Hello World of PHP", $i);//输出 Hxlly Wyrld yf PHP echo $i;//输出4 ?>
Tip
The difference between this function and substr_replace() is that all those that meet the conditions are replaced.
This function is case-sensitive. For case-insensitive search and replace, use str_ireplace()
The above is the detailed content of How to find and replace string in php. For more information, please follow other related articles on the PHP Chinese website!

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

Video Face Swap
Swap faces in any video effortlessly with our completely free AI face swap tool!

Hot Article

Hot Tools

Dreamweaver CS6
Visual web development tools

SAP NetWeaver Server Adapter for Eclipse
Integrate Eclipse with SAP NetWeaver application server.

PhpStorm Mac version
The latest (2018.2.1) professional PHP integrated development tool

Atom editor mac version download
The most popular open source editor

Safe Exam Browser
Safe Exam Browser is a secure browser environment for taking online exams securely. This software turns any computer into a secure workstation. It controls access to any utility and prevents students from using unauthorized resources.
