Php substr replace
From w3cyberlearnings
Contents |
PHP function subsstr_replace
This function replaces a part of a string with another portion of a string.
Syntax subsstr_replace
- string: string to be checked for replacement.
- replacement: new portion of a string to be replaced.
- startpos: positive number (start toward the right-->), negative number(start toward the left<--)
- lengthreplace: positive number (length of a string to replace), negative number (the characters should be left at end of string after replacing), 0 (insert not replace)
subsstr_replace(string, replacement, startpos, lengthreplace);
Example 1
- replace bc with 20,
<?php /////////////////////1 echo substr_replace("abcdef", "20", 1, 2); ?>
Output
a20def
Example 2
- xxxxx=5
<?php ////////1234xxxxx $str = "She loves to read."; echo substr_replace($str, 'does not like', 4, 5); ?>
Output
She does not like to read.
Example 3
- -1: count backward, it is the last character.
<?php echo substr_replace("abcdef", "20", -1, 2); ?>
Output
abcde20
Example 4
- -2=ef: count backward, it is the last two characters.
<?php echo substr_replace("abcdef", "20", -2, 2); ?>
Output
abcd20
Example 5
- 0: insert not replace.
<?php echo substr_replace("abcdef", "230", -2, 0); ?>
Output
abcd230ef