php replace specified string PHP string replacement substr_replace and str_replace function

The substr_replace () function is used to replace part of a string with another string, and returns a mixed type.

echo  substr_replace ('abcdef', '###', 1 );
 // output a ###

echo  substr_replace ('abcdef', '###', 1, 2 );
 // output a ### def

echo  substr_replace ('abcdef', '###', -3, 2 );
 // output abc ### f

echo  substr_replace ('abcdef', '###', 1, -2 );
 // output a ### ef

 

 

The str_replace () function replaces some characters in a string with a string and returns a mixed type.

echo str_replace("world","earth","Hello world!");
//输出 Hello earth!

// Replace multiple, and the second parameter is null character 
echo  str_replace ("o", "", "Hello world!" );
 // Output 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  

 

Guess you like

Origin www.cnblogs.com/zc290987034/p/12707218.html