Tôi đã bắt gặp yêu cầu này gần đây và đưa ra điều này:
/**
* Removes the diacritical marks from a string.
*
* Diacritical marks: {@link //unicode-table.com/blocks/combining-diacritical-marks/}
*
* @param string $string The string from which to strip the diacritical marks.
* @return string Stripped string.
*/
function stripDiacriticalMarks[string $string]: string
{
return preg_replace['/[\x{0300}-\x{036f}]/u', '', \Normalizer::normalize[$string , \Normalizer::FORM_KD]];
}
/**
* Checks if the string $haystack is like $needle, $needle can contain '%' and '_'
* characters which will behave as if used in a SQL LIKE condition. Character escaping
* is supported with '\'.
*
* @param string $haystack The string to check if it is like $needle.
* @param string $needle The string used to check if $haystack is like it.
* @param bool $ai Whether to check likeness in an accent-insensitive manner.
* @param bool $ci Whether to check likeness in a case-insensitive manner.
* @return bool True if $haystack is like $needle, otherwise, false.
*/
function like[string $haystack, string $needle, bool $ai = true, bool $ci = true]: bool
{
if [$ai] {
$haystack = stripDiacriticalMarks[$haystack];
$needle = stripDiacriticalMarks[$needle];
}
$needle = preg_quote[$needle, '/'];
$tokens = [];
$needleLength = strlen[$needle];
for [$i = 0; $i < $needleLength;] {
if [$needle[$i] === '\\'] {
$i += 2;
if [$i < $needleLength] {
if [$needle[$i] === '\\'] {
$tokens[] = '\\\\';
$i += 2;
} else {
$tokens[] = $needle[$i];
++$i;
}
} else {
$tokens[] = '\\\\';
}
} else {
switch [$needle[$i]] {
case '_':
$tokens[] = '.';
break;
case '%':
$tokens[] = '.*';
break;
default:
$tokens[] = $needle[$i];
break;
}
++$i;
}
}
return preg_match['/^' . implode[$tokens] . '$/u' . [$ci ? 'i' : ''], $haystack] === 1;
}
/**
* Escapes a string in a way that `UString::like` will match it as-is, thus '%' and '_'
* would match a literal '%' and '_' respectively [and not behave as in a SQL LIKE
* condition].
*
* @param string $str The string to escape.
* @return string The escaped string.
*/
function escapeLike[string $str]: string
{
return strtr[$str, ['\\' => '\\\\', '%' => '\%', '_' => '\_']];
}
Mã trên là unicode nhận thức để có thể bắt các trường hợp như:
like['Hello 🙃', 'Hello _']; // true
like['Hello 🙃', '_e%o__']; // true
like['asdfas \\🙃H\\\\%🙃É\\l\\_🙃\\l\\o asdfasf', '%' . escapeLike['\\🙃h\\\\%🙃e\\l\\_🙃\\l\\o'] . '%']; // true
Bạn có thể thử tất cả những điều này trên //3v4l.org/o9lx0
[Php 4, Php 5, Php 7, Php 8]
Cấu trúc NO___Trans___Pre___2 là một trong những tính năng quan trọng nhất của nhiều ngôn ngữ, bao gồm PHP. Nó cho phép thực hiện có điều kiện các đoạn mã. PHP có cấu trúc NO___Trans___Pre___2 tương tự như của C:
Như được mô tả trong phần về biểu thức, biểu thức được đánh giá theo giá trị boolean của nó. Nếu biểu thức đánh giá thành NO___Trans___Pre___4, PHP sẽ thực thi câu lệnh và nếu nó đánh giá thành NO___Trans___Pre___5 - nó sẽ bỏ qua nó. Thông tin thêm về những gì các giá trị đánh giá thành NO___Trans___Pre___5 có thể được tìm thấy trong phần 'Chuyển đổi sang Boolean'.expression is evaluated to its Boolean
value. If expression evaluates to true
, PHP will execute statement, and if it evaluates to false
- it'll ignore it. More information about what values evaluate to false
can be found in the 'Converting to boolean' section.
Ví dụ sau đây sẽ hiển thị A lớn hơn B nếu $ A lớn hơn $ B:a is bigger than b if $a is bigger than $b:
Thường thì bạn muốn có nhiều hơn một câu lệnh được thực thi có điều kiện. Tất nhiên, không cần phải bọc mỗi câu lệnh với mệnh đề no___trans___pre___2. Thay vào đó, bạn có thể nhóm một số tuyên bố thành một nhóm tuyên bố. Ví dụ: mã này sẽ hiển thị A lớn hơn B nếu $ A lớn hơn $ B, và sau đó sẽ gán giá trị $ A thành $ B:a is bigger than b if $a is bigger than $b, and would then assign the value of $a into $b:
NO___Trans___Pre___10 Các câu lệnh có thể được lồng vô hạn trong các câu lệnh NO___TRANS___PRE___2 khác, cung cấp cho bạn sự linh hoạt hoàn toàn để thực hiện có điều kiện các phần khác nhau trong chương trình của bạn.
Robk ¶ ¶
9 năm trước
like['Hello 🙃', 'Hello _']; // true
like['Hello 🙃', '_e%o__']; // true
like['asdfas \\🙃H\\\\%🙃É\\l\\_🙃\\l\\o asdfasf', '%' . escapeLike['\\🙃h\\\\%🙃e\\l\\_🙃\\l\\o'] . '%']; // true
2like['Hello 🙃', 'Hello _']; // true
like['Hello 🙃', '_e%o__']; // true
like['asdfas \\🙃H\\\\%🙃É\\l\\_🙃\\l\\o asdfasf', '%' . escapeLike['\\🙃h\\\\%🙃e\\l\\_🙃\\l\\o'] . '%']; // true
3like['Hello 🙃', 'Hello _']; // true
like['Hello 🙃', '_e%o__']; // true
like['asdfas \\🙃H\\\\%🙃É\\l\\_🙃\\l\\o asdfasf', '%' . escapeLike['\\🙃h\\\\%🙃e\\l\\_🙃\\l\\o'] . '%']; // true
4like['Hello 🙃', 'Hello _']; // true
like['Hello 🙃', '_e%o__']; // true
like['asdfas \\🙃H\\\\%🙃É\\l\\_🙃\\l\\o asdfasf', '%' . escapeLike['\\🙃h\\\\%🙃e\\l\\_🙃\\l\\o'] . '%']; // true
5like['Hello 🙃', 'Hello _']; // true
like['Hello 🙃', '_e%o__']; // true
like['asdfas \\🙃H\\\\%🙃É\\l\\_🙃\\l\\o asdfasf', '%' . escapeLike['\\🙃h\\\\%🙃e\\l\\_🙃\\l\\o'] . '%']; // true
6Grawity tại gmail dot com ¶
14 năm trước
like['Hello 🙃', 'Hello _']; // true
like['Hello 🙃', '_e%o__']; // true
like['asdfas \\🙃H\\\\%🙃É\\l\\_🙃\\l\\o asdfasf', '%' . escapeLike['\\🙃h\\\\%🙃e\\l\\_🙃\\l\\o'] . '%']; // true
7like['Hello 🙃', 'Hello _']; // true
like['Hello 🙃', '_e%o__']; // true
like['asdfas \\🙃H\\\\%🙃É\\l\\_🙃\\l\\o asdfasf', '%' . escapeLike['\\🙃h\\\\%🙃e\\l\\_🙃\\l\\o'] . '%']; // true
8like['Hello 🙃', 'Hello _']; // true
like['Hello 🙃', '_e%o__']; // true
like['asdfas \\🙃H\\\\%🙃É\\l\\_🙃\\l\\o asdfasf', '%' . escapeLike['\\🙃h\\\\%🙃e\\l\\_🙃\\l\\o'] . '%']; // true
9if
0
if
1
if
2
like['Hello 🙃', 'Hello _']; // true
like['Hello 🙃', '_e%o__']; // true
like['asdfas \\🙃H\\\\%🙃É\\l\\_🙃\\l\\o asdfasf', '%' . escapeLike['\\🙃h\\\\%🙃e\\l\\_🙃\\l\\o'] . '%']; // true
6TechGuy14 tại Gmail Dot Com ¶ ¶
11 năm trước
if
4
if
5
if
6
like['Hello 🙃', 'Hello _']; // true
like['Hello 🙃', '_e%o__']; // true
like['asdfas \\🙃H\\\\%🙃É\\l\\_🙃\\l\\o asdfasf', '%' . escapeLike['\\🙃h\\\\%🙃e\\l\\_🙃\\l\\o'] . '%']; // true
6Christian L. ¶ ¶
11 năm trước
if
8
if
9
if
0
if
1Christian L. ¶ ¶
Cole Dot Trumbo tại Nospamamthnx dot gmail dot com ¶
if
2
if
3
if
4
if
5
like['Hello 🙃', 'Hello _']; // true
like['Hello 🙃', '_e%o__']; // true
like['asdfas \\🙃H\\\\%🙃É\\l\\_🙃\\l\\o asdfasf', '%' . escapeLike['\\🙃h\\\\%🙃e\\l\\_🙃\\l\\o'] . '%']; // true
65 năm trước ¶
11 năm trước
if
7
if
8
if
9
true
0
like['Hello 🙃', 'Hello _']; // true
like['Hello 🙃', '_e%o__']; // true
like['asdfas \\🙃H\\\\%🙃É\\l\\_🙃\\l\\o asdfasf', '%' . escapeLike['\\🙃h\\\\%🙃e\\l\\_🙃\\l\\o'] . '%']; // true
6