Current Location: Home> Latest Articles> How to use stripos to implement URL keyword recognition in PHP

How to use stripos to implement URL keyword recognition in PHP

M66 2025-06-02

When dealing with URLs and strings, PHP provides a number of built-in functions to simplify our tasks. Among them, the stripos function is particularly useful, which can help us find specific keywords in URLs or other strings. If you want to judge or perform related operations based on certain keywords in the URL, stripos will be a very efficient tool.

What is a scripos function?

The stripos function is a string function in PHP that finds where the specified substring first appears in the target string, and this function is case-insensitive. If no substring is found, stripos returns false .

grammar:

 stripos(string $haystack, string $needle, int $offset = 0): int|false
  • $haystack : Target string.

  • $needle : The substring to be found.

  • $offset : optional, indicating where to start searching.

Use the scripos function to identify keywords in the URL

Suppose we need to identify whether certain keywords are included from a URL. For example, we may want to check whether the URL contains words such as "login", "register", or "admin", which usually appear in the URLs that log in or manage related pages. We can use the stripos function to implement this function.

Here is an example:

 <?php
// Suppose we have one URL
$url = "https://www.m66.net/user/login";

// Define the array of keywords we are looking for
$keywords = ['login', 'register', 'admin'];

// Circular check URL Whether these keywords are included
foreach ($keywords as $keyword) {
    if (stripos($url, $keyword) !== false) {
        echo "URL Includes keywords: $keyword\n";
    } else {
        echo "URL No keywords included: $keyword\n";
    }
}
?>

Code explanation

  1. We first define a string variable $url that contains the URL. In this example, the URL is https://www.m66.net/user/login , but you can replace it with another URL according to the actual situation.

  2. Next, we define a keyword array $keywords that contains the keywords we want to find: login , register , and admin .

  3. We use foreach loop to iterate over the keyword array and use stripos in each loop to find out if the URL contains the keyword. stripos will return the location where the keyword first appears. If false is returned, it means that the keyword is not in the URL.

  4. Based on the return value of stripos , we output the corresponding result, telling the user whether the URL contains the specified keyword.

Why use stripos ?

  1. Case insensitive : The stripos function is case-insensitive, so the case difference of letters can be ignored, making searching more flexible. For example, login and Login will be considered the same.

  2. Return position : stripos will return the position of the target substring in the string. If further operation or positioning is required, the return value is very useful. In contrast, the strpos function is case sensitive and may not be applicable to certain situations.

  3. Easy to use : Stripos is a built-in function with concise syntax and easy to get started, and is very convenient for string search operations.

Combined with actual application

In many practical applications, we need to decide how to handle the request based on the keywords in the URL. For example, we might decide whether the user needs to log in based on whether the URL contains "login" or whether it needs to be redirected to a different page.

For example, the following is a stripos -based URL router example:

 <?php
$url = "https://www.m66.net/admin/dashboard";

if (stripos($url, 'admin') !== false) {
    echo "Enter the administrator interface";
} elseif (stripos($url, 'login') !== false) {
    echo "Enter the login interface";
} else {
    echo "Visit the normal page";
}
?>

In this example, the program checks whether the URL contains "admin" or "login" and outputs different prompts based on the results of the check.

Summarize

The stripos function is a very practical string search tool in PHP. Especially when dealing with URLs, it can help us quickly identify keywords. Whether for routing management or for specific functions, stripos can provide efficient and easy solutions.