Current Location: Home> Latest Articles> How to use stripos to highlight the keywords in the article content

How to use stripos to highlight the keywords in the article content

M66 2025-05-31

When developing a website content management system or displaying articles, it is often necessary to highlight keywords in the article, which can help users find the information they are following more quickly. This requirement can be easily achieved through PHP's scripos function. The stripos function is a case-insensitive string lookup function that finds the location of another string in a specified string. With this, we can iterate through the article content, find the location of the keywords, and wrap it in HTML tags for highlighting.

Step 1: Understand the scripos function

The basic syntax of the stripos function is as follows:

 stripos(string $haystack, string $needle, int $offset = 0): int|false
  • $haystack : The target string to search for, that is, the article content.

  • $needle : The substring to be found, that is, the keywords that need to be highlighted.

  • $offset : From where to look up in the string, the default is 0.

  • Return value: If a keyword is found, it returns the position of the keyword in the target string; if not found, it returns false .

Since stripos is case-insensitive, it is ensured that even if the case of the keyword is different from that in the article, it can be found successfully.

Step 2: Use stripos to achieve keyword highlighting

Next, we will write a function through PHP that takes article content and keywords as parameters to find and highlight all matching keywords.

 <?php
function highlightKeyword($content, $keyword) {
    // Check whether the keyword is empty
    if (empty($keyword)) {
        return $content;
    }

    // use stripos Find keywords,And highlight
    $start = 0;
    while (($pos = stripos($content, $keyword, $start)) !== false) {
        // Insert at the found keyword position HTML Highlight the tag
        $content = substr_replace($content, "<span style='background-color: yellow;'>$keyword</span>", $pos, strlen($keyword));
        // Update search location
        $start = $pos + strlen($keyword);
    }

    return $content;
}

$article = "Welcome to our official website!Here,You can find various about PHP Programming tutorials and resources。";
$keyword = "PHP";
$highlightedArticle = highlightKeyword($article, $keyword);

echo $highlightedArticle;
?>

Step 3: Code parsing

  1. highlightKeyword function : This function receives two parameters, $content and $keyword . Inside the function, we use stripos to find all keywords that appear in the article content, and replace them with substr_replace , and add a yellow background HTML span tag to each matching keyword to achieve highlighting.

  2. Loop search keywords : After finding a keyword in stripos , we use substr_replace to insert HTML tags at that location and wrap keywords. In the next loop, continue to look up from the next position of the current position.

  3. Return result : After loop processing, the function returns the processed content, and all keywords will be highlighted.

Step 4: Code sample output

Suppose our article content is:

 Welcome to our official website!Here,You can find various about PHP Programming tutorials and resources。

If the keyword we pass in is PHP , the output result will be:

 Welcome to our official website!Here,You can find various about <span style='background-color: yellow;'>PHP</span> Programming tutorials and resources。

The keyword PHP will be wrapped by a span tag with a yellow background. When rendered by the browser, the display effect will be highlighted.

Step 5: Customize the highlight style

You can modify the style of the span tag as needed to achieve different highlighting effects. For example, change the background color, font color or bold text. For example:

 $content = substr_replace($content, "<span style='background-color: yellow; font-weight: bold;'>$keyword</span>", $pos, strlen($keyword));

In this way, you can make the highlighting more eye-catching.

Summarize

Through PHP's scripos function, we can easily find and highlight keywords in the article to improve the user experience. This is done by simply nesting strings and HTML tags. You can further expand and optimize according to your needs, such as dealing with multiple keywords, different highlighting styles, etc.