substr_compare - So sánh nhị phân 2 strings từ vị trí offset, tới độ dài ký tự
substr_compare
(PHP 5)
Description
int substr_compare ( string $main_str , string $str , int $offset [, int $length= 0 [, bool $case_insensitivity= false ]] )
substr_compare() compares main_str from position offset with str up to length characters.
Parameters
- offset
-
The start position for the comparison. If negative, it starts counting from the end of the string.
- length
-
The length of the comparison.
- case_insensitivity
-
If case_insensitivity is TRUE, comparison is case insensitive.
Return Values
Returns < 0 if main_str from position offset is less than str , > 0 if it is greater than str , and 0 if they are equal. If length is equal or greater than length of main_str and length is set, substr_compare() prints warning and returns FALSE.
Changelog
| Version | Description |
|---|---|
| 5.1.0 | Added the possibility to use a negative offset . |
Examples
Example #1 A substr_compare() example
<?php
echo substr_compare("abcde", "bc", 1, 2); // 0
echo substr_compare("abcde", "de", -2, 2); // 0
echo substr_compare("abcde", "bcg", 1, 2); // 0
echo substr_compare("abcde", "BC", 1, 2, true); // 0
echo substr_compare("abcde", "bc", 1, 3); // 1
echo substr_compare("abcde", "cd", 1, 2); // -1
echo substr_compare("abcde", "abc", 5, 1); // warning
?> User Contributed Notes
substr_compare
substr_compare
sleek
23-May-2005 12:07
Nguồn php.net
23-May-2005 12:07
Modified version of the original posted function. Use this one:
<?php
if (!function_exists('substr_compare')) {
function substr_compare($main_str, $str, $offset, $length = NULL, $case_insensitivity = false) {
$offset = (int) $offset;
// Throw a warning because the offset is invalid
if ($offset >= strlen($main_str)) {
trigger_error('The start position cannot exceed initial string length.', E_USER_WARNING);
return false;
}
// We are comparing the first n-characters of each string, so let's use the PHP function to do it
if ($offset == 0 && is_int($length) && $case_insensitivity === true) {
return strncasecmp($main_str, $str, $length);
}
// Get the substring that we are comparing
if (is_int($length)) {
$main_substr = substr($main_str, $offset, $length);
$str_substr = substr($str, 0, $length);
} else {
$main_substr = substr($main_str, $offset);
$str_substr = $str;
}
// Return a case-insensitive comparison of the two strings
if ($case_insensitivity === true) {
return strcasecmp($main_substr, $str_substr);
}
// Return a case-sensitive comparison of the two strings
return strcmp($main_substr, $str_substr);
}
}
?> | Chuỗi (xâu) và các hàm xử lý chuỗi trong PHP < Lùi | Tiếp theo > Lập trình PHP - Mã hóa file bằng md5 |
|---|
Tin mới hơn:
- 05/07/2009 16:16 - URI Routing
- 05/07/2009 16:13 - Đăng nhập vào Yahoo sử dụng giao thức YMSG
- 28/06/2009 21:15 - Viết chương trình download manager đơn giản
- 10/06/2009 16:00 - Lập trình PHP - Đếm số người trực tuyến
- 09/06/2009 23:50 - Chuỗi (xâu) và các hàm xử lý chuỗi trong PHP
Tin cũ hơn:
- 08/06/2009 22:24 - Lập trình PHP - Mã hóa file bằng md5
- 08/06/2009 22:18 - Tìm hiểu hàm Function Header trong PHP
- 03/06/2009 23:50 - Lập trình PHP, MySQL - Tạo trang đăng nhập Login
- 03/06/2009 22:00 - Lập trình PHP - Tạo 1 shoutbox đơn giản
- 03/06/2009 18:24 - Chuyển hướng Redirection với Drop Down Menu
Download Game bóng đá PES8 Việt Nam
Tải game bóng đá FiFa 09
Tải game Dragon ball Z - 7 viên ngọc rồng
Tải Game Bóng Đá Pro Evolution Soccer 6 Việt Nam
Download PES 2011 patch Việt Nam PESVN
Tải Game Bóng Đá FIFA 2009 RIP 1 CD
Download Pro Evolution Soccer 2011
Download Game THE SIMS 3
Tải Game bóng đá PES 2009
Download PES 2011 demo
Thay đổi màn hình đăng nhập Windows 7 bằng tay
Một số hàm hay dùng trong JavaScript
Kiểm soát hoàn toàn trỏ chuột bằng bàn phím với NeatMouse
Choáng với concept của Windows Phone 8
Những đặc điểm ưu việt của Windows Phone
Ứng dụng gọi điện VoIP Viber sắp có mặt trên Windows Phone
Game Diablo 3 sắp có mặt trên Windows Phone Marketplace
37 trang css & xhtml web template miễn phí tuyệt đẹp
Những điều cơ bản cần biết về bàn phím chơi game - Gaming Keyboard
L.A. Noire: The Complete Edition - THETA [Full ISO/Action/2011] [13.37GB]
Download 7554 demo
Download Game PC, cập nhật liên tục (torrent)
PES 2012 ra mắt bản mở rộng đầu tiên vào 11/10
PES2012 - Full version - Ngôn ngữ và bình luận tiếng Anh
PES2012 - Blurry disable làm mịn đồ họa
PES 2011 PATCH 4.1 - TRANSFER UPDATE - RELEASED
Bộ sưu tập free PrestaShop Themes phần 3
Bộ sưu tập theme Prestashop miễn phí phần 2
Top 10 Free PrestaShop Themes
Download Prestashop 1.5
Download PrestaShop 1.4
Bổ sung Memcached vào PrestaShop 1.4
Hướng dẫn tích hợp Ngân Lượng vào PrestaShop
Sửa lỗi invalid carrier or no carrier selected trong PrestaShop
Cài đặt PrestaShop
Thiết kế giao diện PrestaShop Theme
Hướng dẫn cài đặt themes trong PrestaShop
Hướng dẫn cách thêm ngôn ngữ cho PrestaShop
Bộ sưu tập Giao diện Premium PrestaShop Themes chuyên nghiệp
Giới thiệu về website thương mại điện tử 2.0 Prestashop
Hướng dẫn chơi Grand Theft Auto: Vice City phần 1
Nero Burning Rom 9.4.26 Portable - Phần mềm ghi đĩa tốt nhất


