[PHP] Hàm substr trong PHP
	
	
		1. Định nghĩa
- Hàm substr() trong PHP dùng để trả về một chuỗi con
2. Cú pháp
	Mã nguồn PHP:
	
substr(string $string, int $offset, ?int $length = null): string 
 3. Tham số
- $string : chuỗi ký tự cha
- $offset : vị trí bắt đầu cắt chuỗi: 
+ $offset > 0 : bắt đầu cắt từ ký tự vị trí $offset
+ $offset < 0 : bắt đầu cắt từ ký tự vị trí cuối chuỗi đảo ngược, tối đa $offset ký tự 
+ $offset = 0 : bắt đầu cắt từ ký tự đầu tiên
- $length : tham số tùy chọn, chỉ định độ dài của chuỗi con:
+ $length > 0 : chiều dài chuỗi con tối đa $length ký tự
+ $length < 0 : bỏ qua tối đa $length ký tự ở cuối chuỗi
+ $length = 0, NULL, or FALSE : trả về chuỗi rỗng
4. Kết quả trả về
- String : Trả về chuỗi con được cắt ra từ chuỗi cha
5. Ví dụ:
- Ví dụ 1: 
+ input:
	Mã nguồn PHP:
	
<?php
// Positive numbers:
echo substr("Hello world",10)."\n";
echo substr("Hello world",1)."\n";
echo substr("Hello world",3)."\n";
echo substr("Hello world",7)."\n";
echo "\n";
// Negative numbers:
echo substr("Hello world",-1)."\n";
echo substr("Hello world",0)."\n";
echo substr("Hello world",0)."\n";
echo substr("Hello world",-4)."\n";
?>
 + output:
	Mã nguồn PHP:
	
d
ello world
lo world
orld
d
Hello world
Hello world
orld 
 - Ví dụ 2: 
+ input:
	Mã nguồn PHP:
	
<?php
// Positive numbers:
echo substr("Hello world",0,10)."\n";
echo substr("Hello world",1,8)."\n";
echo substr("Hello world",0,5)."\n";
echo substr("Hello world",6,6)."\n";
echo "\n";
// Negative numbers:
echo substr("Hello world",0,0)."\n";
echo substr("Hello world",0,-1)."\n";
echo substr("Hello world",-10,-2)."\n";
echo substr("Hello world",0,-6)."\n";
?>
 + output:
	Mã nguồn PHP:
	
Hello worl
ello wor
Hello
world
Hello worl
ello wor
Hello 
 - Example #1 Using a negative offset:
	Mã nguồn PHP:
	
<?php
$rest = substr("abcdef", -1);    // returns "f"
$rest = substr("abcdef", -2);    // returns "ef"
$rest = substr("abcdef", -3, 1); // returns "d"
?>
 - Example #2 Using a negative length:
	Mã nguồn PHP:
	
<?php
$rest = substr("abcdef", 0, -1);  // returns "abcde"
$rest = substr("abcdef", 2, -1);  // returns "cde"
$rest = substr("abcdef", 4, -4);  // returns ""; prior to PHP 8.0.0, false was returned
$rest = substr("abcdef", -3, -1); // returns "de"
?>
 - Example #3 Basic substr() usage:
	Mã nguồn PHP:
	
<?php
echo substr('abcdef', 1);     // bcdef
echo substr("abcdef", 1, null); // bcdef; prior to PHP 8.0.0, empty string was returned
echo substr('abcdef', 1, 3);  // bcd
echo substr('abcdef', 0, 4);  // abcd
echo substr('abcdef', 0, 8);  // abcdef
echo substr('abcdef', -1, 1); // f
// Accessing single characters in a string
// can also be achieved using "square brackets"
$string = 'abcdef';
echo $string[0];                 // a
echo $string[3];                 // d
echo $string[strlen($string)-1]; // f
?>
 6. Tài liệu tham khảo
https://www.php.net/manual/en/function.substr.php
https://www.w3schools.com/php/func_string_substr.asp