Current Location: Home> Latest Articles> Custom functions implement multi-keyword version of stripos

Custom functions implement multi-keyword version of stripos

M66 2025-05-18

In PHP, the stripos function is used to find where a string first appears in another string (ignoring case). However, by default, it can only find one keyword. If we need to find multiple keywords in a string and return their position or match, we need to customize a function to implement this function.

Implementation ideas

We can check the position of each keyword in the target string by iterating through multiple keywords and calling the stripos function. If a keyword is found, the location is recorded. Finally, we can return matching results for all keywords. Here, we will use an array to store the search results for multiple keywords and return their location or other relevant information.

PHP Code Implementation

Below is a simple custom function implementation that supports finding multiple keywords in a string and returning their matching results.

 <?php

/**
 * Custom functions:Support multiple keywords stripos Function
 *
 * @param string $haystack Target string
 * @param array $needles Array of keywords to look for
 * @return array Return an associative array,Contains the location of each keyword
 */
function multi_stripos($haystack, $needles) {
    $results = [];

    foreach ($needles as $needle) {
        $position = stripos($haystack, $needle);
        if ($position !== false) {
            $results[$needle] = $position;
        } else {
            $results[$needle] = null;  // If no keywords are found,return null
        }
    }

    return $results;
}

// Example:Use functions to find multiple keywords
$text = "Welcome to our website,m66.netProvides rich resources。";
$keywords = ["m66.net", "resource", "PHP"];

$matches = multi_stripos($text, $keywords);

// Output result
foreach ($matches as $keyword => $position) {
    if ($position !== null) {
        echo "Keywords '{$keyword}' In location {$position} Found。\n";
    } else {
        echo "Keywords '{$keyword}' not found。\n";
    }
}

?>

Code parsing

  1. Function definition :

    • We define a function called multi_stripos which takes two parameters:

      • $haystack : Target string.

      • $needles : an array containing multiple keywords.

    • Inside the function, we use foreach loop to iterate through each keyword and call the stripos function to find the first occurrence of the keyword in $haystack .

    • If the keyword is found, we save its position in the result array $results . If not found, null is returned, indicating that there is no match.

  2. Function call :

    • In the example, we define a target string $text that contains the URL of the website and some descriptive text.

    • We define an array $keywords containing multiple keywords and pass it to the multi_stripos function to get the location of all keywords.

  3. Results output :

    • We traverse the returned result array and output the matching position of each keyword. If a keyword is not found, we will also output the corresponding prompt information.

Running results

 Keywords 'm66.net' In location 15 Found。
Keywords 'resource' In location 22 Found。
Keywords 'PHP' not found。

Application scenarios

This custom function is very suitable for the following situations:

  • Multiple keyword search : This function is very useful when you need to find multiple keywords in text.

  • Performance optimization : Compared with multiple calls to stripos , this method can centrally handle multiple lookup operations, thereby improving the readability and maintainability of the code.

  • Keyword matching position acquisition : In addition to finding the position, you can also expand the function to return more information, such as the matching full word or its context.

summary

By customizing a stripos function that supports multiple keyword searches, we can effectively solve the problem of finding multiple keywords at the same time. This method is not only simple but also flexible, and can further expand functions according to actual needs.