PHP substr_replace function
The PHP substr_replace function can be used to replace a certain part of the string with other strings. The designer can set the new string to be replaced, where to start from, and the length of the string to be replaced. in PHP 4.3.3 version of the beginning, substr_replace functions have begun to support array array element replacement. Please note that the substr_replace function performs string replacement in the copy, and returns a string result after the processing is completed. If the processing is an array, an array is returned.
PHP substr_replace basic syntax
PHP substr_replace example one, string replacement
$MyString ='ABCDEF';
// Replace the original string with ZZZ
echo substr_replace($MyString,'ZZZ', 0).'<br>';
echo substr_replace($MyString,'ZZZ', 0, strlen ($MyString)).'<br>';
// insert ZZZ at the beginning of the original string
echo substr_replace($MyString,'ZZZ', 0, 0).'<br>';
// replace the original with ZZZ The letters of the string E
echo substr_replace($MyString,'ZZZ', 3, -1).'<br>';
echo substr_replace($MyString,'ZZZ', -3, -1).'<br>';
// Replace the letter D of the original string with a null value
echo substr_replace($MyString,'', 3, 1);
?>
ZZZ
ZZZABCDEF
ABCZZZF
ABCZZZF
ABCEF
PHP substr_replace example two, array replacement
$MyArray = array('A:ZZZ','B:ZZZ','C:ZZZ');
// Replace ZZZ in the original array with YYY
print_r(substr_replace($MyArray,'YYY',2, 3));
// Replace the original array element with the corresponding array element
$replace = array('AAA','BBB','CCC');
print_r(substr_replace($MyArray, $replace,2,3));
/ / Use the value of the array to control the length of the string replaced by the original array element
$length = array(1, 2, 3);
print_r(substr_replace($MyArray,$replace,2,$length));
?>

Post a Comment
0 Comments