substr_replace

Replace text within a portion of a string

Syntax

substr_replace ( mixed $string , mixed $replacement , mixed $start [, mixed $length ] ) : mixed

Parameters

string

The input string. An array of strings can be provided, in which case the replacements will occur on each string in turn. In this case, the replacement, start and length parameters may be provided either as scalar values to be applied to each input string in turn, or as arrays, in which case the corresponding array element will be used for each input string.

replacement

The replacement string.

start

If start is non-negative, the replacing will begin at the start'th offset into string. If start is negative, the replacing will begin at the start'th character from the end of string.

length

If given and is positive, it represents the length of the portion of string which is to be replaced. If it is negative, it represents the number of characters from the end of string at which to stop replacing. If it is not given, then it will default to strlen( string ); i.e. end the replacing at the end of string. Of course, if length is zero then this function will have the effect of inserting replacement into string at the given start offset.

Return

The result string is returned. If string is an array then array is returned.

Examples

1 · string replacement start · Negative

<?

$string = "abcdefghijklmno";
$replacement = "12345";
$start = -10;

$return = substr_replace($string, $replacement, $start);

echo $return;

?>
abcde12345

2 · string replacement start · Non-negative

<?

$string = "abcdefghijklmno";
$replacement = "12345";
$start = 5;

$return = substr_replace($string, $replacement, $start);

echo $return;

?>
abcde12345

3 · length · Negative

<?

$string = "abcdefghijklmno";
$replacement = "12345";
$start = -10;
$length = -5;

$return = substr_replace($string, $replacement, $start, $length);

echo $return;

?>
abcde12345klmno

4 · length · Non-negative

<?

$string = "abcdefghijklmno";
$replacement = "12345";
$start = 5;
$length = 5;

$return = substr_replace($string, $replacement, $start, $length);

echo $return;

?>
abcde12345klmno

5 · Array

<?

$string = array("abc", "def", "ghi", "jkl", "mno");
$replacement = array("1", "2", "3", "4", "5");
$start = 0;
$length = 2;

$return = substr_replace($string, $replacement, $start, $length);

print_r($return);

?>
Array
(
    [0] => 1c
    [1] => 2f
    [2] => 3i
    [3] => 4l
    [4] => 5o
)

addcslashes

addslashes

bin2hex

chop

chr

chunk_split

convert_uudecode

convert_uuencode

count_chars

crc32

crypt

echo

explode

fprintf

get_html_translation_table

hebrev

hebrevc

hex2bin

html_entity_decode

htmlentities

htmlspecialchars

htmlspecialchars_decode

implode

join

lcfirst

levenshtein

localeconv

ltrim

md5

md5_file

metaphone

nl_langinfo

nl2br

number_format

ord

parse_str

print

printf

quoted_printable_decode

quoted_printable_encode

quotemeta

rtrim

setlocale

sha1

sha1_file

similar_text

soundex

sprintf

sscanf

str_contains

str_ends_with

str_getcsv

str_ireplace

str_pad

str_repeat

str_replace

str_rot13

str_shuffle

str_split

str_starts_with

str_word_count

strcasecmp

strchr

strcmp

strcoll

strcspn

strip_tags

stripcslashes

stripos

stripslashes

stristr

strlen

strnatcasecmp

strnatcmp

strncasecmp

strncmp

strpbrk

strpos

strrchr

strrev

strripos

strrpos

strspn

strstr

strtok

strtolower

strtoupper

strtr

substr

substr_compare

substr_count

trim

ucfirst

ucwords

vfprintf

vprintf

vsprintf

wordwrap

PHP

Home Menu