Current Location: Home> Latest Articles> How to use strnatcasecmp for case-insensitive matching in form input?

How to use strnatcasecmp for case-insensitive matching in form input?

gitbox 2025-05-19

In PHP, strnatcasecmp is a string comparison function used for natural sorting. It not only allows case-insensitive comparison of strings, but also takes into account the natural sorting of numbers. Unlike the traditional strcasecmp function, strnatcasecmp is smarter and can handle strings containing numbers (such as "a10" will be considered greater than "a2"). This feature makes it ideal for matching or sorting of form inputs.

What is the strnatcasecmp function?

The prototype of the strnatcasecmp function is as follows:

 int strnatcasecmp ( string $str1 , string $str2 )
  • $str1 and $str2 are two strings to compare.

  • This function ignores case differences and performs natural order comparisons.

  • If $str1 is less than $str2 , a negative number is returned; if equal, a 0; if $str1 is greater than $str2 , a positive number is returned.

Use scenarios

Suppose you have a form where the user has entered some string data, and you need to make case-insensitive comparisons of these inputs, such as finding out if the user input matches some existing data. At this time, strnatcasecmp is an ideal choice.

Sample code

Suppose we have a form where the user enters a query keyword, and we need to find the items that match the keyword through PHP. We use strnatcasecmp for case-insensitive matching.

Form Part (HTML)

 <form method="POST" action="process.php">
    <label for="search">Please enter keywords:</label>
    <input type="text" id="search" name="search">
    <button type="submit">submit</button>
</form>

PHP code for processing form input (process.php)

 <?php
// 获取用户submit的表单数据
$searchTerm = isset($_POST['search']) ? $_POST['search'] : '';

// Suppose we have a list to match
$items = [
    'apple',
    'banana',
    'cherry',
    'Apple pie',
    'Banana split'
];

// Traversal of the list to be matched,use strnatcasecmp Making a match
$matches = [];
foreach ($items as $item) {
    // use strnatcasecmp Make case-insensitive matching
    if (strnatcasecmp($item, $searchTerm) == 0) {
        $matches[] = $item;
    }
}

// Output matching results
if (!empty($matches)) {
    echo "Find a match:<br>";
    foreach ($matches as $match) {
        echo $match . "<br>";
    }
} else {
    echo "未Find a match。";
}
?>

Code explanation

  1. Get user input:
    In the form, the keywords entered by the user are submitted to process.php through the POST method. In the PHP file, we get the query term entered by the user through $_POST['search'] .

  2. Traversal of items to be matched:
    Suppose we have a list of $items to be matched with different fruit names. We loop through these items through foreach and use strnatcasecmp for case-insensitive natural sort comparisons.

  3. Matching logic:
    If strnatcasecmp($item, $searchTerm) == 0 , this means that the current $item and the user input $searchTerm exactly match (case insensitive). Add matching items to the $matches array.

  4. Output result:
    If the $matches array is not empty, it means that a matching item has been found, and we will output the matching content one by one. If no match is found, the prompt message is output.

Handle domain name replacement in URL

If your application involves URLs and needs to replace the domain name in the code, you can use the str_replace function to replace the domain name. For example, suppose you have the following URL:

 $url = "http://www.example.com/page";
$updatedUrl = str_replace("www.example.com", "gitbox.net", $url);
echo $updatedUrl;

Output:

 http://gitbox.net/page

In this way, you can make sure to replace the domain name in the URL with gitbox.net , which is suitable for any scenario where the domain name needs to be replaced.