Hướng dẫn dùng php substr_replace trong PHP

Hàm substr_replace() sẽ thay thế một đoạn của chuỗi bằng một chuỗi nào đó. Cụ thể, hàm sẽ cắt bỏ một đoạn hoặc toàn bộ chuỗi và thay thế đoạn bị cắt bỏ bằng một chuỗi con khác.

Hướng dẫn dùng php substr_replace trong PHP

Hướng dẫn dùng php substr_replace trong PHP

Bài viết này được đăng tại freetuts.net, không được copy dưới mọi hình thức.

Cú pháp

Cú phápsubstr_replace($str, $replace, $pos, $lent);

Trong đó:

  • $str là chuỗi ban đầu cần thay đổi.
  • $replace là chuỗi sẽ được thay thế vào.
  • $pos là vị trí bắt đầu cắt bỏ trong $str.
  • $lent là số kí tính từ vị trí $pos sẽ bị cắt bỏ trong chuỗi $str.

Ví dụ

Sử dụng hàm substr_replace() thay thế toàn bộ chuỗi:

Bài viết này được đăng tại [free tuts .net]

Code

$var = 'This is something';
echo $var . "<br />";
echo substr_replace($var, 'this is replace', 0) . "<br />";
echo substr_replace($var, 'this is replace', 0, strlen($var)) . "<br />";

Kết quả

This is something
this is replace
this is replace

Sử dụng hàm substr_replace() thay thế một đoạn của chuỗi:

Code

$var = 'This is something';
echo $var . "<br />";
echo substr_replace($var, 'replaces', 8) . "<br />";
echo substr_replace($var, 'replaces', 8, 9) . "<br />";

Kết quả

This is something
This is replaces
This is replaces

Sử dụng hàm substr_replace() thay thế nhiều chuỗi cùng một lúc:

Code

$arr = [
	"A: AAA",
	"B: BBB",
	"C: CCC"
];
$result = substr_replace($arr, 'YYY', 3, 3);
echo "<pre>";
	print_r($result);
echo "</pre>";

Kết quả

Array
(
    [0] => A: YYY
    [1] => B: YYY
    [2] => C: YYY
)

Tham khảo: php.net

  • Trang chủ
  • Phát triển web
  • PHP
  • Hàm substr-replace() trong PHP

Hướng dẫn cách sử dụng hàm substr-replace() trong lập trình PHP

Tác dụng của hàm substr-replace()

The substr_replace() function replaces text within a portion of a string.

The following table summarizes the technical details of this function.

Return Value:Returns the replaced string. If string is an array then array is returned.
Version:PHP 4+


Syntax

The basic syntax of the substr_replace() function is given with:

substr_replace(string, replacement, start, length);

The following example shows the substr_replace() function in action.

<?php
// Sample strings
$str = "Fairyland";
$replacement = "Horror";

// Replacing the substring
echo substr_replace($str, $replacement, 0, 5);
?>


Parameters

The substr_replace() function accepts the following parameters.

ParameterDescription
string Required. Specifies the string to work on.
replacement Required. Specifies the replacement string.
start Required. Specifies the position in the string from where the replacement will begin. If it is negative, replacement will begin at the start'th character from the end of string.
length

Optional. Specifies 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 the strlen(string); i.e. length of string.

If it is zero (0) then this function will have the effect of inserting the replacement string into the main string at the given start position.

Tip: You can also pass an array of strings to this function, in this case the replacements will occur on each string. The replacement, start and length parameters may also be provided as arrays, in which case the corresponding array element will be used for each input string.


More Examples

Here're some more examples showing how substr_replace() function actually works:

In the following example portion of string starting from the 6th position till the end will be replaced.

<?php
// Sample strings
$str = "Fairyland";
$replacement = "tales";

// Replacing the substring
echo substr_replace($str, $replacement, 5);
?>

In the following example replacement start at the 4th position from the end of the string.

<?php
// Sample strings
$str = "Wonderland";
$replacement = " Woman";

// Replacing the substring
echo substr_replace($str, $replacement, -4);
?>

In the following example insertion will took place instead of replacement, because length is zero.

<?php
// Sample strings
$str = "Fairyland";
$substr = " Park";

// Inserting the substring
echo substr_replace($str, $substr, 9, 0);
?>

In the following example multiple string will be replaced at once using arrays.

<?php
// Sample inputs
$input = array("A: Apple", "B: Ball", "C: Cat");
$replacement = array("Airplane", "Bus", "Car");

// Replacing the substrings
echo implode("<br>",substr_replace($input, $replacement, 3));
?>

Bài viết này đã giúp ích cho bạn?

Bài viết mới