Home  >  Article  >  Backend Development  >  How to replace characters in a string in php

How to replace characters in a string in php

藏色散人
藏色散人Original
2020-09-30 09:33:1415290browse

php method to replace characters in a string: 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 with other characters in the string.

How to replace characters in a string in php

Recommendation: "PHP Video Tutorial"

PHP String Replacement

is used from Replaces the specified string in the string.

Related functions are as follows:

  • substr_replace(): Replace part of a string with another string
  • str_replace(): Use a string to replace characters Other characters in the string

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 String to be processed
replacement String to be inserted
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 Optional, the length of the string returned, the default is until the end of the string, if it is negative, it will be returned from the end of the string

Example:

b1994d013bac23ea4caf7ffd6145175b

Tip

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

str_replace()

str_replace() function uses a 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] )
Parameter description is as follows:
Parameter Description
search The string to be found (replaced)
replace The string to be replaced with search
string The string to be processed
count Optional, a variable for the replacement count

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  
?>

Prompt

  1. The difference between this function and substr_replace() is that all those that meet the conditions are replaced
  2. This function is case sensitive. For case-insensitive search and replace, use str_ireplace()

The above is the detailed content of How to replace characters in a string 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