(PHP 4, PHP 5, PHP 7)
substr_replace — �滻�ַ������Ӵ�
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
�б��滻�����ַ����ij��ȡ�����趨Ϊ����������ʾ���滻�����ַ�����β������ string
ĩ�˵��ַ����������û���ṩ�˲�������ô��Ĭ��Ϊ strlen( string
) ���ַ����ij��ȣ�����Ȼ����� length
Ϊ 0����ô��������Ĺ���Ϊ�� replacement
���뵽 string
�� start
λ�ô���
���ؽ���ַ�������� string
�Ǹ����飬��ôҲ������һ�����顣
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', 0, strlen($var)) . "<br />\n";
/* �� "bob" ���뵽 $var �Ŀ�ͷ����*/
echo substr_replace($var, 'bob', 0, 0) . "<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', 3, 3))."\n";
// A more complicated case where each replacement is different.
$replace = array('AAA', 'BBB', 'CCC');
echo implode('; ', substr_replace($input, $replace, 3, 3))."\n";
// Replace a different number of characters each time.
$length = array(1, 2, 3);
echo implode('; ', substr_replace($input, $replace, 3, $length))."\n";
?>
�������̻������
A: YYY; B: YYY; C: YYY A: AAA; B: BBB; C: CCC A: AAAXX; B: BBBX; C: CCC
Note: �˺����ɰ�ȫ���ڶ����ƶ���