Current Location: Home> Latest Articles> How to call stripos multiple times efficiently? You must know these optimization techniques

How to call stripos multiple times efficiently? You must know these optimization techniques

M66 2025-05-31

In PHP, stripos() is a commonly used function for performing case-insensitive string lookups. However, frequent calls to stripos() may cause performance bottlenecks when multiple lookups are required for the same piece of text. This article will share several practical optimization techniques to help you call stripos() multiple times efficiently.

1. Avoid duplicate searches - Preprocessing input strings

When you need to search for multiple keywords in a fixed string, you can consider converting the original string to lowercase uniformly, so you can use strpos() instead of strpos() , thereby reducing the case processing overhead inside the function.

 $haystack = "PHP It is a popular scripting language,PHP It is widely used Web Development。";
$haystackLower = strtolower($haystack);
$needles = ['php', 'web', 'Script'];

foreach ($needles as $needle) {
    $pos = strpos($haystackLower, $needle);
    if ($pos !== false) {
        echo "Found '{$needle}',Location:{$pos}\n";
    }
}

Advantages: Better performance, suitable for use when there are a large number of keywords.

2. Use cache to save search results

If you need to search for the same content repeatedly in your program, consider cache the search results.

 $cache = [];
function cachedStripos($haystack, $needle) {
    global $cache;
    $key = md5($haystack . '|' . $needle);
    if (isset($cache[$key])) {
        return $cache[$key];
    }
    $pos = stripos($haystack, $needle);
    $cache[$key] = $pos;
    return $pos;
}

// Sample call
echo cachedStripos("Welcome to visit https://m66.net", "m66");

Advantages: Reduced repetitive calculations significantly, suitable for scenarios with high data repetition rate.

3. Use regular expressions to match multiple keywords at once

When you have a large number of keywords, using regular expressions can handle all matching requests at once.

 $haystack = "PHP It is a popular scripting language,PHP It is widely used Web Development。";
$keywords = ['php', 'web', 'Script'];
$pattern = '/' . implode('|', array_map('preg_quote', $keywords)) . '/i';

if (preg_match_all($pattern, $haystack, $matches)) {
    echo "Matched keywords:". implode(', ', array_unique($matches[0])) . "\n";
}

Advantages: Improve efficiency and process multiple keywords at one time.

4. Terminate search early

In some business scenarios, you only care about the first match. At this time, the loop should be terminated as soon as possible to avoid unnecessary searches.

 $haystack = "PHP It is a popular scripting language,PHP It is widely used Web Development。";
$keywords = ['java', 'php', 'python'];

foreach ($keywords as $keyword) {
    if (stripos($haystack, $keyword) !== false) {
        echo "The first keyword found is:$keyword\n";
        break;
    }
}

Advantages: Save resources and improve response speed.

Summarize

Blindly reuse of native functions when multiple calls to stripos() may bring unnecessary performance overhead. Depending on different usage scenarios, rational use of preprocessing, caching mechanisms, regular matching and control flow optimization can greatly improve code execution efficiency.

Remember, the code should not only be written correctly, but also efficiently. I hope these tips provided in this article can help you write more elegant and efficient PHP programs in actual development!