Hướng dẫn like in if condition php - như trong điều kiện nếu php

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 https://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 https://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:

if ($a $b)
  echo 
"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:

if ($a $b) {
  echo 
"a is bigger than b";
  
$b $a;
}
?>

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
2

like('Hello 🙃', 'Hello _'); // true
like('Hello 🙃', '_e%o__');  // true
like('asdfas \\🙃H\\\\%🙃É\\l\\_🙃\\l\\o asdfasf', '%' . escapeLike('\\🙃h\\\\%🙃e\\l\\_🙃\\l\\o') . '%'); // true
3

like('Hello 🙃', 'Hello _'); // true
like('Hello 🙃', '_e%o__');  // true
like('asdfas \\🙃H\\\\%🙃É\\l\\_🙃\\l\\o asdfasf', '%' . escapeLike('\\🙃h\\\\%🙃e\\l\\_🙃\\l\\o') . '%'); // true
4

like('Hello 🙃', 'Hello _'); // true
like('Hello 🙃', '_e%o__');  // true
like('asdfas \\🙃H\\\\%🙃É\\l\\_🙃\\l\\o asdfasf', '%' . escapeLike('\\🙃h\\\\%🙃e\\l\\_🙃\\l\\o') . '%'); // true
5

like('Hello 🙃', 'Hello _'); // true
like('Hello 🙃', '_e%o__');  // true
like('asdfas \\🙃H\\\\%🙃É\\l\\_🙃\\l\\o asdfasf', '%' . escapeLike('\\🙃h\\\\%🙃e\\l\\_🙃\\l\\o') . '%'); // true
6

Grawity 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
7

like('Hello 🙃', 'Hello _'); // true
like('Hello 🙃', '_e%o__');  // true
like('asdfas \\🙃H\\\\%🙃É\\l\\_🙃\\l\\o asdfasf', '%' . escapeLike('\\🙃h\\\\%🙃e\\l\\_🙃\\l\\o') . '%'); // true
8

like('Hello 🙃', 'Hello _'); // true
like('Hello 🙃', '_e%o__');  // true
like('asdfas \\🙃H\\\\%🙃É\\l\\_🙃\\l\\o asdfasf', '%' . escapeLike('\\🙃h\\\\%🙃e\\l\\_🙃\\l\\o') . '%'); // true
9

if0

if1

if2

like('Hello 🙃', 'Hello _'); // true
like('Hello 🙃', '_e%o__');  // true
like('asdfas \\🙃H\\\\%🙃É\\l\\_🙃\\l\\o asdfasf', '%' . escapeLike('\\🙃h\\\\%🙃e\\l\\_🙃\\l\\o') . '%'); // true
6

TechGuy14 tại Gmail Dot Com ¶

11 năm trước

if4

if5

if6

like('Hello 🙃', 'Hello _'); // true
like('Hello 🙃', '_e%o__');  // true
like('asdfas \\🙃H\\\\%🙃É\\l\\_🙃\\l\\o asdfasf', '%' . escapeLike('\\🙃h\\\\%🙃e\\l\\_🙃\\l\\o') . '%'); // true
6

Christian L. ¶

11 năm trước

if8

if9

if0

if1

Christian L. ¶

Cole Dot Trumbo tại Nospamamthnx dot gmail dot com ¶

if2

if3

if4

if5

like('Hello 🙃', 'Hello _'); // true
like('Hello 🙃', '_e%o__');  // true
like('asdfas \\🙃H\\\\%🙃É\\l\\_🙃\\l\\o asdfasf', '%' . escapeLike('\\🙃h\\\\%🙃e\\l\\_🙃\\l\\o') . '%'); // true
6

5 năm trước

11 năm trước

if7

if8

if9

true0

like('Hello 🙃', 'Hello _'); // true
like('Hello 🙃', '_e%o__');  // true
like('asdfas \\🙃H\\\\%🙃É\\l\\_🙃\\l\\o asdfasf', '%' . escapeLike('\\🙃h\\\\%🙃e\\l\\_🙃\\l\\o') . '%'); // true
6

Làm thế nào để cung cấp nếu điều kiện trong PHP?

Trong PHP, chúng tôi có các câu lệnh có điều kiện sau: nếu câu lệnh - thực thi một số mã nếu một điều kiện là đúng. Nếu ... câu lệnh khác - thực thi một số mã nếu một điều kiện là đúng và mã khác nếu điều kiện đó là sai. Nếu ... otherif ... câu lệnh khác - thực thi các mã khác nhau cho nhiều hơn hai điều kiện.if statement - executes some code if one condition is true. if...else statement - executes some code if a condition is true and another code if that condition is false. if...elseif...else statement - executes different codes for more than two conditions.

Trong PHP như thế nào?

Toán tử tương tự được sử dụng trong mệnh đề WHERE để tìm kiếm một mẫu được chỉ định trong một cột.Có hai ký tự đại diện thường được sử dụng cùng với toán tử tương tự: dấu phần trăm (%) đại diện cho số 0, một hoặc nhiều ký tự.used in a WHERE clause to search for a specified pattern in a column. There are two wildcards often used in conjunction with the LIKE operator: The percent sign (%) represents zero, one, or multiple characters.

Làm gì?: Có nghĩa là trong PHP?

Toán tử độ phân giải phạm vi (còn được gọi là paamayim nekudotayim) hoặc theo cách đơn giản hơn, dấu hai chấm, là một mã thông báo cho phép truy cập vào các thuộc tính hoặc phương thức tĩnh hoặc được ghi đè của một lớp.Scope Resolution Operator (also called Paamayim Nekudotayim) or in simpler terms, the double colon, is a token that allows access to static, constant, and overridden properties or methods of a class.

Làm thế nào để bạn vượt qua hai điều kiện trong một câu lệnh IF trong PHP?

Nếu tuyên bố với nhiều điều kiện trong PHP..
Nếu tuyên bố với điều kiện hoặc (||) trong PHP.$ var = "ABC";if ($ var == "abc" || $ var == "def") {echo "true";} ....
Nếu câu lệnh với điều kiện và (&&) trong PHP.if ($ var == "abc" && $ var2 == "def") {echo "true";} ....
Nếu tuyên bố với Otherif và điều kiện khác trong PHP.