Home Backend Development PHP Tutorial About the method of redlining keywords in text content in PHP

About the method of redlining keywords in text content in PHP

Jun 04, 2018 am 09:18 AM
php Keywords

This article introduces a function in PHP, which can add red to the keywords of the content, highlight the keywords, and share it with everyone. I hope it will be helpful to everyone.

Sometimes when we display an article, we may need to highlight certain keywords in red so that we can quickly find and locate these keywords. Let’s take a look at the specific implementation. code.

/**
  * 关键字套红方法
  * 
  * @access public
  * @param array $options 参数数组
  *  <li> $info_arr  array  内容</li>
  *  <li> $search_arr array  关键词数组 </li>
  * @return int or array
  */
  function set_arr_keyword_red($info_arr, $search_arr)
 {
  foreach ($search_arr as $search_str)
  {
   foreach ($info_arr as $key => $info)
   {
    if(in_array($key,array('item_title','keywords', 'photo_title', 'photo_site','content',)))
    {
     $info = strip_tags($info);
     $info = str_replace(' ', '', $info);
     $q_str_pos = stripos($info, $search_str);
     if (false!==$q_str_pos)
     {
      $info = csubstr($info, $q_str_pos+150);
      $temp = csubstr($info,$q_str_pos-150);
      $info = substr($info, strlen($temp), 300);
      $info = preg_replace("/{$search_str}/i", "<font color='red'>{$search_str}</font>", $info);
 
      if($info_arr['match_key']=='')
      $info_arr['match_key'] = $key;
     }
     else
     {
      $info = csubstr($info,300);
     }
    }
    $info_arr[$key] = $info;
   }
  }
  return $info_arr;
 }
 
$str = 'woloveu小君';
$info_arr = array('photo_title' => 'womejiojd我们都围殴防静低屁啊小君鸡冻啊泡脚盆大');
$search_arr = array('小君');
$ret = set_arr_keyword_red($info_arr, $search_arr);
dump($ret );
Copy after login

Summary: The above is the entire content of this article, I hope it will be helpful to everyone's study.

Related recommendations: Detailed explanation of the definition and usage examples of similar_text() function in

phpPHP array Summary of basic knowledge of functions

php

Usage and examples of cookie information security for user login

The above is the detailed content of About the method of redlining keywords in text content in PHP. For more information, please follow other related articles on the PHP Chinese website!

Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn

Hot Article Tags

Notepad++7.3.1

Notepad++7.3.1

Easy-to-use and free code editor

SublimeText3 Chinese version

SublimeText3 Chinese version

Chinese version, very easy to use

Zend Studio 13.0.1

Zend Studio 13.0.1

Powerful PHP integrated development environment

Dreamweaver CS6

Dreamweaver CS6

Visual web development tools

SublimeText3 Mac version

SublimeText3 Mac version

God-level code editing software (SublimeText3)

CakePHP Date and Time CakePHP Date and Time Sep 10, 2024 pm 05:27 PM

CakePHP Date and Time

PHP 8.4 Installation and Upgrade guide for Ubuntu and Debian PHP 8.4 Installation and Upgrade guide for Ubuntu and Debian Dec 24, 2024 pm 04:42 PM

PHP 8.4 Installation and Upgrade guide for Ubuntu and Debian

CakePHP File upload CakePHP File upload Sep 10, 2024 pm 05:27 PM

CakePHP File upload

CakePHP Routing CakePHP Routing Sep 10, 2024 pm 05:25 PM

CakePHP Routing

Discuss CakePHP Discuss CakePHP Sep 10, 2024 pm 05:28 PM

Discuss CakePHP

CakePHP Project Configuration CakePHP Project Configuration Sep 10, 2024 pm 05:25 PM

CakePHP Project Configuration

CakePHP Quick Guide CakePHP Quick Guide Sep 10, 2024 pm 05:27 PM

CakePHP Quick Guide

How To Set Up Visual Studio Code (VS Code) for PHP Development How To Set Up Visual Studio Code (VS Code) for PHP Development Dec 20, 2024 am 11:31 AM

How To Set Up Visual Studio Code (VS Code) for PHP Development

See all articles