substr_replace

(PHP 4, PHP 5, PHP 7)

substr_replace锟芥换锟街凤拷锟斤拷锟斤拷锟接达拷

说锟斤拷

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

substr_replace() 锟斤拷锟街凤拷锟斤拷 string 锟侥革拷锟斤拷锟叫斤拷锟斤拷 start 锟酵匡拷选锟斤拷 length 锟斤拷锟斤拷锟睫讹拷锟斤拷锟斤拷锟街凤拷锟斤拷使锟斤拷 replacement 锟斤拷锟斤拷锟芥换锟斤拷

锟斤拷锟斤拷

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

锟芥换锟街凤拷锟斤拷锟斤拷

start

锟斤拷锟� start 为锟斤拷锟斤拷锟斤拷锟芥换锟斤拷锟斤拷 string 锟斤拷 start 位锟矫匡拷始锟斤拷

锟斤拷锟� start 为锟斤拷锟斤拷锟斤拷锟芥换锟斤拷锟斤拷 string 锟侥碉拷锟斤拷锟斤拷 start 锟斤拷位锟矫匡拷始锟斤拷

length

锟斤拷锟斤拷瓒拷锟斤拷锟斤拷锟斤拷锟斤拷锟斤拷锟斤拷为锟斤拷锟斤拷锟斤拷锟斤拷示 string 锟叫憋拷锟芥换锟斤拷锟斤拷锟街凤拷锟斤拷锟侥筹拷锟饺★拷锟斤拷锟斤拷瓒ㄎ拷锟斤拷锟斤拷锟斤拷锟斤拷锟绞撅拷锟斤拷婊伙拷锟斤拷锟斤拷址锟斤拷锟斤拷锟轿诧拷锟斤拷锟斤拷锟� string 末锟剿碉拷锟街凤拷锟斤拷锟斤拷锟斤拷锟斤拷锟矫伙拷锟斤拷峁╋拷瞬锟斤拷锟斤拷锟斤拷锟矫达拷锟侥拷锟轿� strlen( string ) 锟斤拷锟街凤拷锟斤拷锟侥筹拷锟饺o拷锟斤拷锟斤拷然锟斤拷锟斤拷锟� length 为 0锟斤拷锟斤拷么锟斤拷锟斤拷锟斤拷锟斤拷墓锟斤拷锟轿拷锟� replacement 锟斤拷锟诫到 string 锟斤拷 start 位锟矫达拷锟斤拷

锟斤拷锟斤拷值

锟斤拷锟截斤拷锟斤拷址锟斤拷锟斤拷锟斤拷锟斤拷 string 锟角革拷锟斤拷锟介,锟斤拷么也锟斤拷锟斤拷锟斤拷一锟斤拷锟斤拷锟介。

锟斤拷锟斤拷锟斤拷志

锟芥本 说锟斤拷
4.3.3 All parameters now accept arrays.

锟斤拷锟斤拷

Example #1 substr_replace() 锟斤拷锟斤拷

<?php
$var 
'ABCDEFGH:/MNRPQR/';
echo 
"Original: $var<hr />\n";

/* 锟斤拷锟斤拷锟斤拷锟斤拷锟斤拷使锟斤拷 "bob" 锟芥换锟斤拷锟斤拷 $var锟斤拷*/
echo substr_replace($var'bob'0) . "<br />\n";
echo 
substr_replace($var'bob'0strlen($var)) . "<br />\n";

/* 锟斤拷 "bob" 锟斤拷锟诫到 $var 锟侥匡拷头锟斤拷锟斤拷*/
echo substr_replace($var'bob'00) . "<br />\n";

/* 锟斤拷锟斤拷锟斤拷锟斤拷锟斤拷锟斤拷使锟斤拷 "bob" 锟芥换 $var 锟叫碉拷 "MNRPQR"锟斤拷*/
echo substr_replace($var'bob'10, -1) . "<br />\n";
echo 
substr_replace($var'bob', -7, -1) . "<br />\n";

/* 锟斤拷 $var 锟斤拷删锟斤拷 "MNRPQR"锟斤拷*/
echo substr_replace($var''10, -1) . "<br />\n";
?>

Example #2 Using substr_replace() to replace multiple strings at once

<?php
$input 
= array('A: XXX''B: XXX''C: XXX');

// A simple case: replace XXX in each string with YYY.
echo implode('; 'substr_replace($input'YYY'33))."\n";

// A more complicated case where each replacement is different.
$replace = array('AAA''BBB''CCC');
echo 
implode('; 'substr_replace($input$replace33))."\n";

// Replace a different number of characters each time.
$length = array(123);
echo 
implode('; 'substr_replace($input$replace3$length))."\n";
?>

锟斤拷锟斤拷锟斤拷锟教伙拷锟斤拷锟斤拷锟�

A: YYY; B: YYY; C: YYY
A: AAA; B: BBB; C: CCC
A: AAAXX; B: BBBX; C: CCC

注锟斤拷

Note: 锟剿猴拷锟斤拷锟缴帮拷全锟斤拷锟节讹拷锟斤拷锟狡讹拷锟斤拷

锟轿硷拷