[PHP] Hàm substr trong PHP

ID Topic: 39126 • Có 90 lượt xem
Hình đại diện của thành viên
Ubuntu (Off) ♀️
Cấp độ: ☀️1/30☀️ ♡3♡
Like: 0/0
Online: ✨5/5364✨
????? ⚡??/??⚡
🩸3/4139🩸 🌟0/1310🌟
#1 - @620826
28/11/2024 01:06
1. Định nghĩa
- Hàm substr() trong PHP dùng để trả về một chuỗi con

2. Cú pháp
Mã: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ã:<?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ã:d
ello world
lo world
orld

d
Hello world
Hello world
orld


- Ví dụ 2:
+ input:
Mã:<?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ã:Hello worl
ello wor
Hello
world


Hello worl
ello wor
Hello


- Example #1 Using a negative offset:
Mã:<?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ã:<?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ã:<?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
(Google Chrome Browser)
PM | Trích dẫn | Like bài viết | Sửa | Xóa | Báo cáo | Cảnh cáo
Sửa lần cuối: Ubuntu 28/11/2024 15:00
Trả lời nhanh

Chủ đề tương tự