Home > Backend Development > PHP Tutorial > php string replacement method

php string replacement method

angryTom
Release: 2023-04-07 08:50:01
Original
16136 people have browsed it

php string replacement method

String replacement is a data processing method often used in the development process. Below we will introduce to you the replacement method of character creation in php.

Recommended tutorial: PHP video tutorial

Related functions are as follows:

substr_replace(): Replace part of a string with another string

str_replace(): Use a String replaces other characters in the string

substr_replace()

substr_replace() function is used to replace part of the string with Another string, returned as a mixed type.

Syntax:

mix substr_replace ( mixed string, string replacement, int start [, int length] )
Copy after login

Parameter description is as follows:

Parameters Description
stringThe string to be processed
replacementThe string to be inserted
startThe 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
lengthOptional, the length returned by the string, the default is until the end of the string, if it is negative, it will be returned from the end of the string

Example :

<?php
echo substr_replace(&#39;abcdef&#39;, &#39;###&#39;, 1);	//输出 a###
echo substr_replace(&#39;abcdef&#39;, &#39;###&#39;, 1, 2);	//输出 a###def
echo substr_replace(&#39;abcdef&#39;, &#39;###&#39;, -3, 2);	//输出 abc###f
echo substr_replace(&#39;abcdef&#39;, &#39;###&#39;, 1, -2);	//输出 a###ef 
?>
Copy after login

Tips

If start is a negative number and length is less than or equal to start, length is 0.

str_replace()

The 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] )
Copy after login

Parameter description is as follows:

Parameter Description
searchThe string to be found (replaced)
replaceString to replace search
stringString to be processed
count Optional, a variable that counts substitutions
##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  
?>
Copy after login

Prompt

This The difference between the 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 php string replacement method. 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