answer
1421
Bùi Hà ·Bùi Hà· Bùi Hà
12:24 07/05/2009
function startsWith[$haystack, $needle]
{
$length = strlen[$needle];
return [substr[$haystack, 0, $length] === $needle];
}
function endsWith[$haystack, $needle]
{
$length = strlen[$needle];
if [$length == 0] {
return true;
}
return [substr[$haystack, -$length] === $needle];
}
Sử dụng cái này nếu bạn không muốn sử dụng regex.
1421 hữu ích 5 bình luận chia sẻ 5 bình luận chia sẻ
answer
953
Có thể sử dụng
4và echo 'generating tests';
for[$i = 0; $i $time] {
echo "$func: " . number_format[$time, 1] . " ms\n";
}
5kiểm tra bắt đầu và kết thúc tương ứng.echo 'generating tests';
for[$i = 0; $i $time] {
echo "$func: " . number_format[$time, 1] . " ms\n";
}
echo 'generating tests';
for[$i = 0; $i $time] {
echo "$func: " . number_format[$time, 1] . " ms\n";
}
4và echo 'generating tests';
for[$i = 0; $i $time] {
echo "$func: " . number_format[$time, 1] . " ms\n";
}
5kiểm tra bắt đầu và kết thúc tương ứng.Lưu ý rằng việc sử dụng
4để kiểm tra bắt đầu bằng và echo 'generating tests';
for[$i = 0; $i $time] {
echo "$func: " . number_format[$time, 1] . " ms\n";
}
5kiểm tra kết thúc bằng sẽ trả về càng sớm càng tốt thay vì kiểm tra toàn bộ chuỗi cho đến khi kết thúc. Ngoài ra, giải pháp này không tạo ra một chuỗi tạm thời. Xem xét giải thích lý do trước khi hạ cấp. Chỉ vì một fit wit tại DWTF không hiểu cách thức hoạt động của chức năng này hoặc nghĩ rằng chỉ có một giải pháp không có nghĩa là câu trả lời này sai.echo 'generating tests';
for[$i = 0; $i $time] {
echo "$func: " . number_format[$time, 1] . " ms\n";
}
4để kiểm tra bắt đầu bằng và
echo 'generating tests';
for[$i = 0; $i $time] {
echo "$func: " . number_format[$time, 1] . " ms\n";
}
5kiểm tra kết thúc bằng sẽ trả về càng sớm càng tốt thay vì kiểm tra toàn bộ chuỗi cho đến khi kết thúc. Ngoài ra, giải pháp này không tạo ra một chuỗi tạm thời. Xem xét giải thích lý do trước khi hạ cấp. Chỉ vì một fit wit tại DWTF không hiểu cách thức hoạt động của chức năng này hoặc nghĩ rằng chỉ có một giải pháp không có nghĩa là câu trả lời này sai.echo 'generating tests';
for[$i = 0; $i $time] {
echo "$func: " . number_format[$time, 1] . " ms\n";
}
function startsWith[$haystack, $needle] {
// search backwards starting from haystack length characters from the end
return $needle === ''
|| strrpos[$haystack, $needle, -strlen[$haystack]] !== false;
}
function endsWith[$haystack, $needle] {
// search forward starting from end minus needle length characters
if [$needle === ''] {
return true;
}
$diff = \strlen[$haystack] - \strlen[$needle];
return $diff >= 0 && strpos[$haystack, $needle, $diff] !== false;
}
Các xét nghiệm và kết quả [ so sánh với điều này ]:so sánh với điều này ]:
startsWith['abcdef', 'ab'] -> true
startsWith['abcdef', 'cd'] -> false
startsWith['abcdef', 'ef'] -> false
startsWith['abcdef', ''] -> true
startsWith['', 'abcdef'] -> false
endsWith['abcdef', 'ab'] -> false
endsWith['abcdef', 'cd'] -> false
endsWith['abcdef', 'ef'] -> true
endsWith['abcdef', ''] -> true
endsWith['', 'abcdef'] -> false
Lưu ý: các chức năng
8và echo 'generating tests';
for[$i = 0; $i $time] {
echo "$func: " . number_format[$time, 1] . " ms\n";
}
9sẽ tốt hơn chức năng này.chức năng echo 'generating tests';
for[$i = 0; $i $time] {
echo "$func: " . number_format[$time, 1] . " ms\n";
}
echo 'generating tests';
for[$i = 0; $i $time] {
echo "$func: " . number_format[$time, 1] . " ms\n";
}
8và echo 'generating tests';
for[$i = 0; $i $time] {
echo "$func: " . number_format[$time, 1] . " ms\n";
}
9sẽ tốt hơn chức năng này.953 hữu ích 5 bình luận chia sẻ 5 bình luận chia sẻ
answer
225
Cập nhật ngày 23 tháng 8 năm 2016
Chức năng
function substr_startswith[$haystack, $needle] {
return substr[$haystack, 0, strlen[$needle]] === $needle;
}
function preg_match_startswith[$haystack, $needle] {
return preg_match['~' . preg_quote[$needle, '~'] . '~A', $haystack] > 0;
}
function substr_compare_startswith[$haystack, $needle] {
return substr_compare[$haystack, $needle, 0, strlen[$needle]] === 0;
}
function strpos_startswith[$haystack, $needle] {
return strpos[$haystack, $needle] === 0;
}
function strncmp_startswith[$haystack, $needle] {
return strncmp[$haystack, $needle, strlen[$needle]] === 0;
}
function strncmp_startswith2[$haystack, $needle] {
return $haystack[0] === $needle[0]
? strncmp[$haystack, $needle, strlen[$needle]] === 0
: false;
}
Xét nghiệm
echo 'generating tests';
for[$i = 0; $i $time] {
echo "$func: " . number_format[$time, 1] . " ms\n";
}
Kết quả [PHP 7.0.9]
[Sắp xếp nhanh nhất đến chậm nhất]
strncmp_startswith2: 40.2 ms
strncmp_startswith: 42.9 ms
substr_compare_startswith: 44.5 ms
substr_startswith: 48.4 ms
strpos_startswith: 138.7 ms
preg_match_startswith: 13,152.4 ms
Kết quả [PHP 5.3,29]
[Sắp xếp nhanh nhất đến chậm nhất]
strncmp_startswith2: 477.9 ms
strpos_startswith: 522.1 ms
strncmp_startswith: 617.1 ms
substr_compare_startswith: 706.7 ms
substr_startswith: 756.8 ms
preg_match_startswith: 10,200.0 ms
startedwith_benchmark.php
Kết quả [PHP 5.3,29] 5 bình luận chia sẻ
answer
131
225 hữu ích 5 bình luận chia sẻ
strncmp_startswith2: 40.2 ms
strncmp_startswith: 42.9 ms
substr_compare_startswith: 44.5 ms
substr_startswith: 48.4 ms
strpos_startswith: 138.7 ms
preg_match_startswith: 13,152.4 ms
0, strncmp_startswith2: 40.2 ms
strncmp_startswith: 42.9 ms
substr_compare_startswith: 44.5 ms
substr_startswith: 48.4 ms
strpos_startswith: 138.7 ms
preg_match_startswith: 13,152.4 ms
1, vv strncmp_startswith2: 40.2 ms
strncmp_startswith: 42.9 ms
substr_compare_startswith: 44.5 ms
substr_startswith: 48.4 ms
strpos_startswith: 138.7 ms
preg_match_startswith: 13,152.4 ms
2và strncmp_startswith2: 40.2 ms
strncmp_startswith: 42.9 ms
substr_compare_startswith: 44.5 ms
substr_startswith: 48.4 ms
strpos_startswith: 138.7 ms
preg_match_startswith: 13,152.4 ms
3chức năng trả lại chỉ số của sự xuất hiện đầu tiên của strncmp_startswith2: 40.2 ms
strncmp_startswith: 42.9 ms
substr_compare_startswith: 44.5 ms
substr_startswith: 48.4 ms
strpos_startswith: 138.7 ms
preg_match_startswith: 13,152.4 ms
4năm strncmp_startswith2: 40.2 ms
strncmp_startswith: 42.9 ms
substr_compare_startswith: 44.5 ms
substr_startswith: 48.4 ms
strpos_startswith: 138.7 ms
preg_match_startswith: 13,152.4 ms
5:function startsWith[$haystack,$needle,$case=true]
{
if [$case]
return strpos[$haystack, $needle, 0] === 0;
return stripos[$haystack, $needle, 0] === 0;
}
function endsWith[$haystack,$needle,$case=true]
{
$expectedPosition = strlen[$haystack] - strlen[$needle];
if [$case]
return strrpos[$haystack, $needle, 0] === $expectedPosition;
return strripos[$haystack, $needle, 0] === $expectedPosition;
}
Tất cả các câu trả lời dường như cho đến nay để làm vô số việc không cần thiết,
0, strncmp_startswith2: 40.2 ms
strncmp_startswith: 42.9 ms
substr_compare_startswith: 44.5 ms
substr_startswith: 48.4 ms
strpos_startswith: 138.7 ms
preg_match_startswith: 13,152.4 ms
1, vv strncmp_startswith2: 40.2 ms
strncmp_startswith: 42.9 ms
substr_compare_startswith: 44.5 ms
substr_startswith: 48.4 ms
strpos_startswith: 138.7 ms
preg_match_startswith: 13,152.4 ms
2và strncmp_startswith2: 40.2 ms
strncmp_startswith: 42.9 ms
substr_compare_startswith: 44.5 ms
substr_startswith: 48.4 ms
strpos_startswith: 138.7 ms
preg_match_startswith: 13,152.4 ms
3chức năng trả lại chỉ số của sự xuất hiện đầu tiên của strncmp_startswith2: 40.2 ms
strncmp_startswith: 42.9 ms
substr_compare_startswith: 44.5 ms
substr_startswith: 48.4 ms
strpos_startswith: 138.7 ms
preg_match_startswith: 13,152.4 ms
4năm strncmp_startswith2: 40.2 ms
strncmp_startswith: 42.9 ms
substr_compare_startswith: 44.5 ms
substr_startswith: 48.4 ms
strpos_startswith: 138.7 ms
preg_match_startswith: 13,152.4 ms
5: 5 bình luận chia sẻ strncmp_startswith2: 40.2 ms
strncmp_startswith: 42.9 ms
substr_compare_startswith: 44.5 ms
substr_startswith: 48.4 ms
strpos_startswith: 138.7 ms
preg_match_startswith: 13,152.4 ms
answer
45
function startsWith[$haystack, $needle, $case = true] {
if [$case] {
return [strcmp[substr[$haystack, 0, strlen[$needle]], $needle] === 0];
}
return [strcasecmp[substr[$haystack, 0, strlen[$needle]], $needle] === 0];
}
function endsWith[$haystack, $needle, $case = true] {
if [$case] {
return [strcmp[substr[$haystack, strlen[$haystack] - strlen[$needle]], $needle] === 0];
}
return [strcasecmp[substr[$haystack, strlen[$haystack] - strlen[$needle]], $needle] === 0];
}
131 hữu ích 5 bình luận chia sẻ :
Tín dụng vào :
Kiểm tra xem một chuỗi kết thúc bằng một chuỗi khác
Kiểm tra xem một chuỗi bắt đầu bằng một chuỗi khác 3 bình luận chia sẻ
answer
26
45 hữu ích 3 bình luận chia sẻ
function startsWith[$needle, $haystack] {
return preg_match['/^' . preg_quote[$needle, '/'] . '/', $haystack];
}
function endsWith[$needle, $haystack] {
return preg_match['/' . preg_quote[$needle, '/'] . '$/', $haystack];
}
Các hàm regex ở trên, nhưng với các chỉnh sửa khác cũng được đề xuất ở trên: 1 bình luận chia sẻ
answer
21
26 hữu ích 1 bình luận chia sẻ
Nếu tốc độ là quan trọng đối với bạn, hãy thử điều này. [Tôi tin rằng đó là phương pháp nhanh nhất]
function startsWith[$haystack, $needle] {
// search backwards starting from haystack length characters from the end
return $needle === ''
|| strrpos[$haystack, $needle, -strlen[$haystack]] !== false;
}
function endsWith[$haystack, $needle] {
// search forward starting from end minus needle length characters
if [$needle === ''] {
return true;
}
$diff = \strlen[$haystack] - \strlen[$needle];
return $diff >= 0 && strpos[$haystack, $needle, $diff] !== false;
}
0Chỉ hoạt động cho chuỗi và nếu $ haystack chỉ có 1 ký tự 3 bình luận chia sẻ
answer
21
21 hữu ích 3 bình luận chia sẻ Nếu chuỗi bạn đang tìm kiếm được biết đến [mã hóa cứng], bạn có thể sử dụng các biểu thức thông thường mà không cần trích dẫn, v.v.
Câu hỏi này đã có nhiều câu trả lời, nhưng trong một số trường hợp, bạn có thể giải quyết cho một cái gì đó đơn giản hơn tất cả chúng. Nếu chuỗi bạn đang tìm kiếm được biết đến [mã hóa cứng], bạn có thể sử dụng các biểu thức thông thường mà không cần trích dẫn, v.v.
function startsWith[$haystack, $needle] {
// search backwards starting from haystack length characters from the end
return $needle === ''
|| strrpos[$haystack, $needle, -strlen[$haystack]] !== false;
}
function endsWith[$haystack, $needle] {
// search forward starting from end minus needle length characters
if [$needle === ''] {
return true;
}
$diff = \strlen[$haystack] - \strlen[$needle];
return $diff >= 0 && strpos[$haystack, $needle, $diff] !== false;
}
1Kiểm tra xem một chuỗi bắt đầu bằng 'ABC':
function startsWith[$haystack, $needle] {
// search backwards starting from haystack length characters from the end
return $needle === ''
|| strrpos[$haystack, $needle, -strlen[$haystack]] !== false;
}
function endsWith[$haystack, $needle] {
// search forward starting from end minus needle length characters
if [$needle === ''] {
return true;
}
$diff = \strlen[$haystack] - \strlen[$needle];
return $diff >= 0 && strpos[$haystack, $needle, $diff] !== false;
}
2kết thúc bằng 'ABC':
function startsWith[$haystack, $needle] {
// search backwards starting from haystack length characters from the end
return $needle === ''
|| strrpos[$haystack, $needle, -strlen[$haystack]] !== false;
}
function endsWith[$haystack, $needle] {
// search forward starting from end minus needle length characters
if [$needle === ''] {
return true;
}
$diff = \strlen[$haystack] - \strlen[$needle];
return $diff >= 0 && strpos[$haystack, $needle, $diff] !== false;
}
3Trong trường hợp đơn giản của tôi, tôi muốn kiểm tra xem một chuỗi kết thúc bằng dấu gạch chéo:
strncmp_startswith2: 40.2 ms
strncmp_startswith: 42.9 ms
substr_compare_startswith: 44.5 ms
substr_startswith: 48.4 ms
strpos_startswith: 138.7 ms
preg_match_startswith: 13,152.4 ms
6] như được hiển thị ở trên.Ưu điểm: vì nó rất ngắn và đơn giản, bạn không phải xác định hàm [chẳng hạn như
6] như được hiển thị ở trên.strncmp_startswith2: 40.2 ms
strncmp_startswith: 42.9 ms
substr_compare_startswith: 44.5 ms
substr_startswith: 48.4 ms
strpos_startswith: 138.7 ms
preg_match_startswith: 13,152.4 ms
Nhưng một lần nữa - đây không phải là một giải pháp cho mọi trường hợp, chỉ là điều này rất cụ thể. 4 bình luận chia sẻ
answer
16
21 hữu ích 4 bình luận chia sẻ
function startsWith[$haystack, $needle] {
// search backwards starting from haystack length characters from the end
return $needle === ''
|| strrpos[$haystack, $needle, -strlen[$haystack]] !== false;
}
function endsWith[$haystack, $needle] {
// search forward starting from end minus needle length characters
if [$needle === ''] {
return true;
}
$diff = \strlen[$haystack] - \strlen[$needle];
return $diff >= 0 && strpos[$haystack, $needle, $diff] !== false;
}
4Đây là hai hàm không giới thiệu một chuỗi tạm thời, có thể hữu ích khi kim rất lớn: 4 bình luận chia sẻ
answer
15
16 hữu ích 4 bình luận chia sẻ strncmp vì nó cho phép bạn đặt độ dài của chuỗi để so sánh với, vì vậy:
function startsWith[$haystack, $needle] {
// search backwards starting from haystack length characters from the end
return $needle === ''
|| strrpos[$haystack, $needle, -strlen[$haystack]] !== false;
}
function endsWith[$haystack, $needle] {
// search forward starting from end minus needle length characters
if [$needle === ''] {
return true;
}
$diff = \strlen[$haystack] - \strlen[$needle];
return $diff >= 0 && strpos[$haystack, $needle, $diff] !== false;
}
5Tôi nhận ra điều này đã được hoàn thành, nhưng bạn có thể muốn xem strncmp vì nó cho phép bạn đặt độ dài của chuỗi để so sánh với, vì vậy: 4 bình luận chia sẻ
answer
10
15 hữu ích 4 bình luận chia sẻ
5vàecho 'generating tests';
for[$i = 0; $i $time] {
echo "$func: " . number_format[$time, 1] . " ms\n";
}
4echo 'generating tests';
for[$i = 0; $i $time] {
echo "$func: " . number_format[$time, 1] . " ms\n";
}
function startsWith[$haystack, $needle] {
// search backwards starting from haystack length characters from the end
return $needle === ''
|| strrpos[$haystack, $needle, -strlen[$haystack]] !== false;
}
function endsWith[$haystack, $needle] {
// search forward starting from end minus needle length characters
if [$needle === ''] {
return true;
}
$diff = \strlen[$haystack] - \strlen[$needle];
return $diff >= 0 && strpos[$haystack, $needle, $diff] !== false;
}
6Bạn có thể sử dụng
5vàecho 'generating tests';
for[$i = 0; $i $time] {
echo "$func: " . number_format[$time, 1] . " ms\n";
}
4 1 bình luận chia sẻ echo 'generating tests';
for[$i = 0; $i $time] {
echo "$func: " . number_format[$time, 1] . " ms\n";
}
answer
8
10 hữu ích 1 bình luận chia sẻ
function startsWith[$haystack, $needle] {
// search backwards starting from haystack length characters from the end
return $needle === ''
|| strrpos[$haystack, $needle, -strlen[$haystack]] !== false;
}
function endsWith[$haystack, $needle] {
// search forward starting from end minus needle length characters
if [$needle === ''] {
return true;
}
$diff = \strlen[$haystack] - \strlen[$needle];
return $diff >= 0 && strpos[$haystack, $needle, $diff] !== false;
}
7Kết thúc nhanh nhất với giải pháp With []:
function startsWith[$haystack, $needle] {
// search backwards starting from haystack length characters from the end
return $needle === ''
|| strrpos[$haystack, $needle, -strlen[$haystack]] !== false;
}
function endsWith[$haystack, $needle] {
// search forward starting from end minus needle length characters
if [$needle === ''] {
return true;
}
$diff = \strlen[$haystack] - \strlen[$needle];
return $diff >= 0 && strpos[$haystack, $needle, $diff] !== false;
}
8Điểm chuẩn:
function startsWith[$haystack, $needle] {
// search backwards starting from haystack length characters from the end
return $needle === ''
|| strrpos[$haystack, $needle, -strlen[$haystack]] !== false;
}
function endsWith[$haystack, $needle] {
// search forward starting from end minus needle length characters
if [$needle === ''] {
return true;
}
$diff = \strlen[$haystack] - \strlen[$needle];
return $diff >= 0 && strpos[$haystack, $needle, $diff] !== false;
}
9Kết quả điểm chuẩn: 2 bình luận chia sẻ
answer
7
8 hữu ích 2 bình luận chia sẻ
Ngắn và dễ hiểu một lớp lót mà không cần biểu thức thông thường.
startsWith['abcdef', 'ab'] -> true
startsWith['abcdef', 'cd'] -> false
startsWith['abcdef', 'ef'] -> false
startsWith['abcdef', ''] -> true
startsWith['', 'abcdef'] -> false
endsWith['abcdef', 'ab'] -> false
endsWith['abcdef', 'cd'] -> false
endsWith['abcdef', 'ef'] -> true
endsWith['abcdef', ''] -> true
endsWith['', 'abcdef'] -> false
0startedWith [] là thẳng về phía trước.
startsWith['abcdef', 'ab'] -> true
startsWith['abcdef', 'cd'] -> false
startsWith['abcdef', 'ef'] -> false
startsWith['abcdef', ''] -> true
startsWith['', 'abcdef'] -> false
endsWith['abcdef', 'ab'] -> false
endsWith['abcdef', 'cd'] -> false
endsWith['abcdef', 'ef'] -> true
endsWith['abcdef', ''] -> true
endsWith['', 'abcdef'] -> false
1endWith [] sử dụng strrev [] hơi lạ mắt và chậm: 3 bình luận chia sẻ
answer
6
7 hữu ích 3 bình luận chia sẻ
startsWith['abcdef', 'ab'] -> true
startsWith['abcdef', 'cd'] -> false
startsWith['abcdef', 'ef'] -> false
startsWith['abcdef', ''] -> true
startsWith['', 'abcdef'] -> false
endsWith['abcdef', 'ab'] -> false
endsWith['abcdef', 'cd'] -> false
endsWith['abcdef', 'ef'] -> true
endsWith['abcdef', ''] -> true
endsWith['', 'abcdef'] -> false
2Tập trung vào startwith, nếu bạn chắc chắn các chuỗi không trống, hãy thêm một bài kiểm tra vào char đầu tiên, trước khi so sánh, strlen, v.v., tăng tốc mọi thứ lên một chút:Thêm một thử nghiệm char khác, như $ haystack {1} === $ kim {1} dường như không tăng tốc mọi thứ nhiều, thậm chí có thể chậm lại.
Đó là bằng cách nào đó [20% -30%] nhanh hơn. Thêm một thử nghiệm char khác, như $ haystack {1} === $ kim {1} dường như không tăng tốc mọi thứ nhiều, thậm chí có thể chậm lại.có vẻ nhanh hơn
strncmp_startswith2: 477.9 ms
strpos_startswith: 522.1 ms
strncmp_startswith: 617.1 ms
substr_compare_startswith: 706.7 ms
substr_startswith: 756.8 ms
preg_match_startswith: 10,200.0 ms
0 toán tử strncmp_startswith2: 477.9 ms
strpos_startswith: 522.1 ms
strncmp_startswith: 617.1 ms
substr_compare_startswith: 706.7 ms
substr_startswith: 756.8 ms
preg_match_startswith: 10,200.0 ms
1có điều kiện dường như nhanh hơnstrncmp_startswith2: 477.9 ms
strpos_startswith: 522.1 ms
strncmp_startswith: 617.1 ms
substr_compare_startswith: 706.7 ms
substr_startswith: 756.8 ms
preg_match_startswith: 10,200.0 ms
2
9có vẻ nhanh hơn strncmp_startswith2: 40.2 ms
strncmp_startswith: 42.9 ms
substr_compare_startswith: 44.5 ms
substr_startswith: 48.4 ms
strpos_startswith: 138.7 ms
preg_match_startswith: 13,152.4 ms
0 toán tử strncmp_startswith2: 477.9 ms
strpos_startswith: 522.1 ms
strncmp_startswith: 617.1 ms
substr_compare_startswith: 706.7 ms
substr_startswith: 756.8 ms
preg_match_startswith: 10,200.0 ms
1có điều kiện dường như nhanh hơnstrncmp_startswith2: 477.9 ms
strpos_startswith: 522.1 ms
strncmp_startswith: 617.1 ms
substr_compare_startswith: 706.7 ms
substr_startswith: 756.8 ms
preg_match_startswith: 10,200.0 ms
2gọi các giải pháp khác là "công việc không cần thiết"strncmp_startswith2: 477.9 ms
strpos_startswith: 522.1 ms
strncmp_startswith: 617.1 ms
substr_compare_startswith: 706.7 ms
substr_startswith: 756.8 ms
preg_match_startswith: 10,200.0 ms
Đối với những người hỏi "tại sao không sử dụng strpose?" gọi các giải pháp khác là "công việc không cần thiết"
strpose là nhanh, nhưng nó không phải là công cụ phù hợp cho công việc này.
startsWith['abcdef', 'ab'] -> true
startsWith['abcdef', 'cd'] -> false
startsWith['abcdef', 'ef'] -> false
startsWith['abcdef', ''] -> true
startsWith['', 'abcdef'] -> false
endsWith['abcdef', 'ab'] -> false
endsWith['abcdef', 'cd'] -> false
endsWith['abcdef', 'ef'] -> true
endsWith['abcdef', ''] -> true
endsWith['', 'abcdef'] -> false
3Để hiểu, đây là một mô phỏng nhỏ làm ví dụ:
startsWith['abcdef', 'ab'] -> true
startsWith['abcdef', 'cd'] -> false
startsWith['abcdef', 'ef'] -> false
startsWith['abcdef', ''] -> true
startsWith['', 'abcdef'] -> false
endsWith['abcdef', 'ab'] -> false
endsWith['abcdef', 'cd'] -> false
endsWith['abcdef', 'ef'] -> true
endsWith['abcdef', ''] -> true
endsWith['', 'abcdef'] -> false
4Những gì máy tính làm "bên trong"?
Giả sử strlen không lặp lại toàn bộ chuỗi [nhưng ngay cả trong trường hợp đó] thì điều này không thuận tiện chút nào. 4 bình luận chia sẻ
answer
6
6 hữu ích 4 bình luận chia sẻ gạch dưới-php ngày nay.
startsWith['abcdef', 'ab'] -> true
startsWith['abcdef', 'cd'] -> false
startsWith['abcdef', 'ef'] -> false
startsWith['abcdef', ''] -> true
startsWith['', 'abcdef'] -> false
endsWith['abcdef', 'ab'] -> false
endsWith['abcdef', 'cd'] -> false
endsWith['abcdef', 'ef'] -> true
endsWith['abcdef', ''] -> true
endsWith['', 'abcdef'] -> false
5Tôi thường kết thúc với một thư viện như gạch dưới-php ngày nay.
Thư viện có đầy đủ các chức năng tiện dụng khác. 0 bình luận chia sẻ
answer
6
6 hữu ích 0 bình luận chia sẻ trả lời của mpen là vô cùng kỹ lưỡng, nhưng, thật không may, điểm chuẩn được cung cấp có một sự giám sát rất quan trọng và bất lợi.
Bởi vì mỗi byte trong kim và đống cỏ là hoàn toàn ngẫu nhiên, xác suất một cặp kim-haystack sẽ khác nhau trên byte đầu tiên là 99,609375%, có nghĩa là, trung bình, khoảng 99609 trong số 100000 cặp sẽ khác nhau ở byte đầu tiên . Nói cách khác, điểm chuẩn rất thiên về các
3triển khai kiểm tra byte đầu tiên một cách rõ ràng strncmp_startswith2: 477.9 ms
strpos_startswith: 522.1 ms
strncmp_startswith: 617.1 ms
substr_compare_startswith: 706.7 ms
substr_startswith: 756.8 ms
preg_match_startswith: 10,200.0 ms
4.Nói cách khác, điểm chuẩn rất thiên về các strncmp_startswith2: 477.9 ms
strpos_startswith: 522.1 ms
strncmp_startswith: 617.1 ms
substr_compare_startswith: 706.7 ms
substr_startswith: 756.8 ms
preg_match_startswith: 10,200.0 ms
strncmp_startswith2: 477.9 ms
strpos_startswith: 522.1 ms
strncmp_startswith: 617.1 ms
substr_compare_startswith: 706.7 ms
substr_startswith: 756.8 ms
preg_match_startswith: 10,200.0 ms
3triển khai kiểm tra byte đầu tiên một cách rõ ràng strncmp_startswith2: 477.9 ms
strpos_startswith: 522.1 ms
strncmp_startswith: 617.1 ms
substr_compare_startswith: 706.7 ms
substr_startswith: 756.8 ms
preg_match_startswith: 10,200.0 ms
4.Nếu vòng lặp tạo thử nghiệm được thực hiện như sau:
startsWith['abcdef', 'ab'] -> true
startsWith['abcdef', 'cd'] -> false
startsWith['abcdef', 'ef'] -> false
startsWith['abcdef', ''] -> true
startsWith['', 'abcdef'] -> false
endsWith['abcdef', 'ab'] -> false
endsWith['abcdef', 'cd'] -> false
endsWith['abcdef', 'ef'] -> true
endsWith['abcdef', ''] -> true
endsWith['', 'abcdef'] -> false
6kết quả điểm chuẩn cho một câu chuyện hơi khác:
startsWith['abcdef', 'ab'] -> true
startsWith['abcdef', 'cd'] -> false
startsWith['abcdef', 'ef'] -> false
startsWith['abcdef', ''] -> true
startsWith['', 'abcdef'] -> false
endsWith['abcdef', 'ab'] -> false
endsWith['abcdef', 'cd'] -> false
endsWith['abcdef', 'ef'] -> true
endsWith['abcdef', ''] -> true
endsWith['', 'abcdef'] -> false
7Tất nhiên, điểm chuẩn này có thể vẫn không hoàn toàn thiên vị, nhưng nó cũng kiểm tra hiệu quả của các thuật toán khi được cung cấp một phần kim phù hợp.
6 hữu ích 0 bình luận chia sẻ 0 bình luận chia sẻ
answer
6
Đây là phiên bản an toàn nhiều byte của câu trả lời được chấp nhận, nó hoạt động tốt cho các chuỗi UTF-8:
startsWith['abcdef', 'ab'] -> true
startsWith['abcdef', 'cd'] -> false
startsWith['abcdef', 'ef'] -> false
startsWith['abcdef', ''] -> true
startsWith['', 'abcdef'] -> false
endsWith['abcdef', 'ab'] -> false
endsWith['abcdef', 'cd'] -> false
endsWith['abcdef', 'ef'] -> true
endsWith['abcdef', ''] -> true
endsWith['', 'abcdef'] -> false
86 hữu ích 2 bình luận chia sẻ 2 bình luận chia sẻ
answer
5
Đây là phiên bản an toàn nhiều byte của câu trả lời được chấp nhận, nó hoạt động tốt cho các chuỗi UTF-8:
startsWith['abcdef', 'ab'] -> true
startsWith['abcdef', 'cd'] -> false
startsWith['abcdef', 'ef'] -> false
startsWith['abcdef', ''] -> true
startsWith['', 'abcdef'] -> false
endsWith['abcdef', 'ab'] -> false
endsWith['abcdef', 'cd'] -> false
endsWith['abcdef', 'ef'] -> true
endsWith['abcdef', ''] -> true
endsWith['', 'abcdef'] -> false
96 hữu ích 2 bình luận chia sẻ 0 bình luận chia sẻ
answer
4
Đây là phiên bản an toàn nhiều byte của câu trả lời được chấp nhận, nó hoạt động tốt cho các chuỗi UTF-8:
strncmp_startswith2: 477.9 ms
strpos_startswith: 522.1 ms
strncmp_startswith: 617.1 ms
substr_compare_startswith: 706.7 ms
substr_startswith: 756.8 ms
preg_match_startswith: 10,200.0 ms
5chức năng có thể trở lại strncmp_startswith2: 477.9 ms
strpos_startswith: 522.1 ms
strncmp_startswith: 617.1 ms
substr_compare_startswith: 706.7 ms
substr_startswith: 756.8 ms
preg_match_startswith: 10,200.0 ms
6trong nhiều trường hợp đặc biệt, vì vậy đây là phiên bản của tôi, trong đó đề với những vấn đề này:function substr_startswith[$haystack, $needle] {
return substr[$haystack, 0, strlen[$needle]] === $needle;
}
function preg_match_startswith[$haystack, $needle] {
return preg_match['~' . preg_quote[$needle, '~'] . '~A', $haystack] > 0;
}
function substr_compare_startswith[$haystack, $needle] {
return substr_compare[$haystack, $needle, 0, strlen[$needle]] === 0;
}
function strpos_startswith[$haystack, $needle] {
return strpos[$haystack, $needle] === 0;
}
function strncmp_startswith[$haystack, $needle] {
return strncmp[$haystack, $needle, strlen[$needle]] === 0;
}
function strncmp_startswith2[$haystack, $needle] {
return $haystack[0] === $needle[0]
? strncmp[$haystack, $needle, strlen[$needle]] === 0
: false;
}
06 hữu ích 2 bình luận chia sẻ
strncmp_startswith2: 477.9 ms
strpos_startswith: 522.1 ms
strncmp_startswith: 617.1 ms
substr_compare_startswith: 706.7 ms
substr_startswith: 756.8 ms
preg_match_startswith: 10,200.0 ms
7có nghĩa là tốt]:Tôi hy vọng rằng câu trả lời dưới đây có thể hiệu quả và cũng đơn giản:5 hữu ích 0 bình luận chia sẻ
echo 'generating tests';
for[$i = 0; $i $time] {
echo "$func: " . number_format[$time, 1] . " ms\n";
}
9chức năng cũng đáng tìm kiếm. //www.php.net/manual/en/feft.substr-compare.phpCác
5chức năng có thể trở lại strncmp_startswith2: 477.9 ms
strpos_startswith: 522.1 ms
strncmp_startswith: 617.1 ms
substr_compare_startswith: 706.7 ms
substr_startswith: 756.8 ms
preg_match_startswith: 10,200.0 ms
6trong nhiều trường hợp đặc biệt, vì vậy đây là phiên bản của tôi, trong đó đề với những vấn đề này: 0 bình luận chia sẻ strncmp_startswith2: 477.9 ms
strpos_startswith: 522.1 ms
strncmp_startswith: 617.1 ms
substr_compare_startswith: 706.7 ms
substr_startswith: 756.8 ms
preg_match_startswith: 10,200.0 ms
answer
4
Đây là phiên bản an toàn nhiều byte của câu trả lời được chấp nhận, nó hoạt động tốt cho các chuỗi UTF-8:
6 hữu ích 2 bình luận chia sẻCác
5chức năng có thể trở lại strncmp_startswith2: 477.9 ms
strpos_startswith: 522.1 ms
strncmp_startswith: 617.1 ms
substr_compare_startswith: 706.7 ms
substr_startswith: 756.8 ms
preg_match_startswith: 10,200.0 ms
6trong nhiều trường hợp đặc biệt, vì vậy đây là phiên bản của tôi, trong đó đề với những vấn đề này: 0 bình luận chia sẻ strncmp_startswith2: 477.9 ms
strpos_startswith: 522.1 ms
strncmp_startswith: 617.1 ms
substr_compare_startswith: 706.7 ms
substr_startswith: 756.8 ms
preg_match_startswith: 10,200.0 ms
answer
3
Đây là phiên bản an toàn nhiều byte của câu trả lời được chấp nhận, nó hoạt động tốt cho các chuỗi UTF-8:
function substr_startswith[$haystack, $needle] {
return substr[$haystack, 0, strlen[$needle]] === $needle;
}
function preg_match_startswith[$haystack, $needle] {
return preg_match['~' . preg_quote[$needle, '~'] . '~A', $haystack] > 0;
}
function substr_compare_startswith[$haystack, $needle] {
return substr_compare[$haystack, $needle, 0, strlen[$needle]] === 0;
}
function strpos_startswith[$haystack, $needle] {
return strpos[$haystack, $needle] === 0;
}
function strncmp_startswith[$haystack, $needle] {
return strncmp[$haystack, $needle, strlen[$needle]] === 0;
}
function strncmp_startswith2[$haystack, $needle] {
return $haystack[0] === $needle[0]
? strncmp[$haystack, $needle, strlen[$needle]] === 0
: false;
}
36 hữu ích 2 bình luận chia sẻ //stackoverflow.com/a/4419658
Tôi hy vọng rằng câu trả lời dưới đây có thể hiệu quả và cũng đơn giản: 0 bình luận chia sẻ
answer
3
Đây là phiên bản an toàn nhiều byte của câu trả lời được chấp nhận, nó hoạt động tốt cho các chuỗi UTF-8:
function substr_startswith[$haystack, $needle] {
return substr[$haystack, 0, strlen[$needle]] === $needle;
}
function preg_match_startswith[$haystack, $needle] {
return preg_match['~' . preg_quote[$needle, '~'] . '~A', $haystack] > 0;
}
function substr_compare_startswith[$haystack, $needle] {
return substr_compare[$haystack, $needle, 0, strlen[$needle]] === 0;
}
function strpos_startswith[$haystack, $needle] {
return strpos[$haystack, $needle] === 0;
}
function strncmp_startswith[$haystack, $needle] {
return strncmp[$haystack, $needle, strlen[$needle]] === 0;
}
function strncmp_startswith2[$haystack, $needle] {
return $haystack[0] === $needle[0]
? strncmp[$haystack, $needle, strlen[$needle]] === 0
: false;
}
46 hữu ích 2 bình luận chia sẻ
Tôi hy vọng rằng câu trả lời dưới đây có thể hiệu quả và cũng đơn giản:
5 hữu ích 0 bình luận chia sẻ
echo 'generating tests';
for[$i = 0; $i $time] {
echo "$func: " . number_format[$time, 1] . " ms\n";
}
5sẽ trả về false nếu không
tìm thấy kim trong đống cỏ khô và sẽ trả về 0 nếu và chỉ khi tìm thấy kim ở chỉ số 0 [AKA bắt đầu].Các
5chức năng có thể trở lại strncmp_startswith2: 477.9 ms
strpos_startswith: 522.1 ms
strncmp_startswith: 617.1 ms
substr_compare_startswith: 706.7 ms
substr_startswith: 756.8 ms
preg_match_startswith: 10,200.0 ms
6trong nhiều trường hợp đặc biệt, vì vậy đây là phiên bản của tôi, trong đó đề với những vấn đề này:strncmp_startswith2: 477.9 ms
strpos_startswith: 522.1 ms
strncmp_startswith: 617.1 ms
substr_compare_startswith: 706.7 ms
substr_startswith: 756.8 ms
preg_match_startswith: 10,200.0 ms
function substr_startswith[$haystack, $needle] {
return substr[$haystack, 0, strlen[$needle]] === $needle;
}
function preg_match_startswith[$haystack, $needle] {
return preg_match['~' . preg_quote[$needle, '~'] . '~A', $haystack] > 0;
}
function substr_compare_startswith[$haystack, $needle] {
return substr_compare[$haystack, $needle, 0, strlen[$needle]] === 0;
}
function strpos_startswith[$haystack, $needle] {
return strpos[$haystack, $needle] === 0;
}
function strncmp_startswith[$haystack, $needle] {
return strncmp[$haystack, $needle, strlen[$needle]] === 0;
}
function strncmp_startswith2[$haystack, $needle] {
return $haystack[0] === $needle[0]
? strncmp[$haystack, $needle, strlen[$needle]] === 0
: false;
}
5Các xét nghiệm [
7có nghĩa là tốt]:strncmp_startswith2: 477.9 ms
strpos_startswith: 522.1 ms
strncmp_startswith: 617.1 ms
substr_compare_startswith: 706.7 ms
substr_startswith: 756.8 ms
preg_match_startswith: 10,200.0 ms
function substr_startswith[$haystack, $needle] {
return substr[$haystack, 0, strlen[$needle]] === $needle;
}
function preg_match_startswith[$haystack, $needle] {
return preg_match['~' . preg_quote[$needle, '~'] . '~A', $haystack] > 0;
}
function substr_compare_startswith[$haystack, $needle] {
return substr_compare[$haystack, $needle, 0, strlen[$needle]] === 0;
}
function strpos_startswith[$haystack, $needle] {
return strpos[$haystack, $needle] === 0;
}
function strncmp_startswith[$haystack, $needle] {
return strncmp[$haystack, $needle, strlen[$needle]] === 0;
}
function strncmp_startswith2[$haystack, $needle] {
return $haystack[0] === $needle[0]
? strncmp[$haystack, $needle, strlen[$needle]] === 0
: false;
}
6
1 function substr_startswith[$haystack, $needle] {
return substr[$haystack, 0, strlen[$needle]] === $needle;
}
function preg_match_startswith[$haystack, $needle] {
return preg_match['~' . preg_quote[$needle, '~'] . '~A', $haystack] > 0;
}
function substr_compare_startswith[$haystack, $needle] {
return substr_compare[$haystack, $needle, 0, strlen[$needle]] === 0;
}
function strpos_startswith[$haystack, $needle] {
return strpos[$haystack, $needle] === 0;
}
function strncmp_startswith[$haystack, $needle] {
return strncmp[$haystack, $needle, strlen[$needle]] === 0;
}
function strncmp_startswith2[$haystack, $needle] {
return $haystack[0] === $needle[0]
? strncmp[$haystack, $needle, strlen[$needle]] === 0
: false;
}
Ngoài ra,
9chức năng cũng đáng tìm kiếm. //www.php.net/manual/en/feft.substr-compare.phpecho 'generating tests';
for[$i = 0; $i $time] {
echo "$func: " . number_format[$time, 1] . " ms\n";
}
4 hữu ích 0 bình luận chia sẻ 4 bình luận chia sẻ
answer
3
Đây là phiên bản an toàn nhiều byte của câu trả lời được chấp nhận, nó hoạt động tốt cho các chuỗi UTF-8:
function substr_startswith[$haystack, $needle] {
return substr[$haystack, 0, strlen[$needle]] === $needle;
}
function preg_match_startswith[$haystack, $needle] {
return preg_match['~' . preg_quote[$needle, '~'] . '~A', $haystack] > 0;
}
function substr_compare_startswith[$haystack, $needle] {
return substr_compare[$haystack, $needle, 0, strlen[$needle]] === 0;
}
function strpos_startswith[$haystack, $needle] {
return strpos[$haystack, $needle] === 0;
}
function strncmp_startswith[$haystack, $needle] {
return strncmp[$haystack, $needle, strlen[$needle]] === 0;
}
function strncmp_startswith2[$haystack, $needle] {
return $haystack[0] === $needle[0]
? strncmp[$haystack, $needle, strlen[$needle]] === 0
: false;
}
76 hữu ích 2 bình luận chia sẻ 1 bình luận chia sẻ
answer
3
Đây là phiên bản an toàn nhiều byte của câu trả lời được chấp nhận, nó hoạt động tốt cho các chuỗi UTF-8:
function substr_startswith[$haystack, $needle] {
return substr[$haystack, 0, strlen[$needle]] === $needle;
}
function preg_match_startswith[$haystack, $needle] {
return preg_match['~' . preg_quote[$needle, '~'] . '~A', $haystack] > 0;
}
function substr_compare_startswith[$haystack, $needle] {
return substr_compare[$haystack, $needle, 0, strlen[$needle]] === 0;
}
function strpos_startswith[$haystack, $needle] {
return strpos[$haystack, $needle] === 0;
}
function strncmp_startswith[$haystack, $needle] {
return strncmp[$haystack, $needle, strlen[$needle]] === 0;
}
function strncmp_startswith2[$haystack, $needle] {
return $haystack[0] === $needle[0]
? strncmp[$haystack, $needle, strlen[$needle]] === 0
: false;
}
86 hữu ích 2 bình luận chia sẻ ngay lập tức , do đó làm cho nhiều so sánh của bạn nhanh hơn rất nhiều [nhanh hơn 7 lần khi tôi đo]. Trong trường hợp thực tế, bạn hầu như không phải trả giá về hiệu suất cho dòng đơn đó vì vậy tôi nghĩ rằng nó đáng để bao gồm. [Ngoài ra, trong thực tế, khi bạn kiểm tra nhiều chuỗi cho một đoạn bắt đầu cụ thể, hầu hết các so sánh sẽ thất bại vì trong trường hợp điển hình là bạn đang tìm kiếm thứ gì đó.]
Tôi hy vọng rằng câu trả lời dưới đây có thể hiệu quả và cũng đơn giản: 2 bình luận chia sẻ
answer
2
Đây là phiên bản an toàn nhiều byte của câu trả lời được chấp nhận, nó hoạt động tốt cho các chuỗi UTF-8:
function substr_startswith[$haystack, $needle] {
return substr[$haystack, 0, strlen[$needle]] === $needle;
}
function preg_match_startswith[$haystack, $needle] {
return preg_match['~' . preg_quote[$needle, '~'] . '~A', $haystack] > 0;
}
function substr_compare_startswith[$haystack, $needle] {
return substr_compare[$haystack, $needle, 0, strlen[$needle]] === 0;
}
function strpos_startswith[$haystack, $needle] {
return strpos[$haystack, $needle] === 0;
}
function strncmp_startswith[$haystack, $needle] {
return strncmp[$haystack, $needle, strlen[$needle]] === 0;
}
function strncmp_startswith2[$haystack, $needle] {
return $haystack[0] === $needle[0]
? strncmp[$haystack, $needle, strlen[$needle]] === 0
: false;
}
96 hữu ích 2 bình luận chia sẻ
Tôi hy vọng rằng câu trả lời dưới đây có thể hiệu quả và cũng đơn giản: 2 bình luận chia sẻ
answer
2
Đây là phiên bản an toàn nhiều byte của câu trả lời được chấp nhận, nó hoạt động tốt cho các chuỗi UTF-8:
echo 'generating tests';
for[$i = 0; $i $time] {
echo "$func: " . number_format[$time, 1] . " ms\n";
}
06 hữu ích 2 bình luận chia sẻ 1 bình luận chia sẻ
answer
2
Đây là phiên bản an toàn nhiều byte của câu trả lời được chấp nhận, nó hoạt động tốt cho các chuỗi UTF-8:Tuy nhiên, điều này có thể ngắn như bạn có thể làm cho nó và để nó làm những gì bạn mong muốn. Bạn chỉ cần nói rằng bạn muốn nó 'trở về đúng'. Vì vậy, tôi đã bao gồm các giải pháp trả về boolean đúng / sai và văn bản đúng / sai.
echo 'generating tests';
for[$i = 0; $i $time] {
echo "$func: " . number_format[$time, 1] . " ms\n";
}
16 hữu ích 2 bình luận chia sẻ 4 bình luận chia sẻ
answer
1
Tôi hy vọng rằng câu trả lời dưới đây có thể hiệu quả và cũng đơn giản:
echo 'generating tests';
for[$i = 0; $i $time] {
echo "$func: " . number_format[$time, 1] . " ms\n";
}
9thay vì function startsWith[$haystack,$needle,$case=true]
{
if [$case]
return strpos[$haystack, $needle, 0] === 0;
return stripos[$haystack, $needle, 0] === 0;
}
function endsWith[$haystack,$needle,$case=true]
{
$expectedPosition = strlen[$haystack] - strlen[$needle];
if [$case]
return strrpos[$haystack, $needle, 0] === $expectedPosition;
return strripos[$haystack, $needle, 0] === $expectedPosition;
}
1.
echo 'generating tests';
for[$i = 0; $i $time] {
echo "$func: " . number_format[$time, 1] . " ms\n";
}
25 hữu ích 0 bình luận chia sẻ 0 bình luận chia sẻ
answer
1
echo 'generating tests';
for[$i = 0; $i $time] {
echo "$func: " . number_format[$time, 1] . " ms\n";
}
3Các
5chức năng có thể trở lại strncmp_startswith2: 477.9 ms
strpos_startswith: 522.1 ms
strncmp_startswith: 617.1 ms
substr_compare_startswith: 706.7 ms
substr_startswith: 756.8 ms
preg_match_startswith: 10,200.0 ms
6trong nhiều trường hợp đặc biệt, vì vậy đây là phiên bản của tôi, trong đó đề với những vấn đề này: 2 bình luận chia sẻ strncmp_startswith2: 477.9 ms
strpos_startswith: 522.1 ms
strncmp_startswith: 617.1 ms
substr_compare_startswith: 706.7 ms
substr_startswith: 756.8 ms
preg_match_startswith: 10,200.0 ms