Use PHP to highlight perfect keywords

Source: Internet
Author: User

<? Php
/*
Usage: highlight keywords
Requirement: replace & lt; and & gt; with all <and> characters except HTML tags in the document for future reference.
$ Rows ['content'] = str_replace ("<", "& lt;", $ rows [content]);
$ Rows ['content'] = str_replace (">", "& gt;", $ rows [content]);
There may be a problem: the efficiency is not high, and the case-sensitivity conversion problem is not forgotten.
$ Content: the reference document to be highlighted
$ Key: keyword
*/
Function highlight ($ content, $ key ){
$ K_fi = substr ($ key,); // obtain the first character of a keyword.
$ K_len = strlen ($ key); // calculates the number of keywords.
$ L_len = strlen ($ content); // calculates the number of words for future reference.
For ($ l_n = 0; $ l_n <$ l_len; $ l_n ++) // starts a loop based on the number of words in the prepared document.
{
$ L_s = substr ($ content, $ l_n, 1); // get the current character of the prepared document
If ($ l_s = "<") // if this character is the start of the tag
{
While ($ l_s! = ">") // We will find the tag close.
{
$ Con. = $ l_s; // import result
$ L_n ++; // Of course, you must start to take the next character of the document for future reference.
$ L_s = substr ($ content, $ l_n, 1 );
}
$ Con. = $ l_s;
}
Elseif ($ l_s = $ k_fi) // if this character is the same as the first character of the keyword
{
$ L_key = substr ($ content, $ l_n, $ k_len); // specifies whether the current position of the document matches the keyword.
If ($ l_key! = $ Key)
{
$ Con. = $ l_s; // import result
}
Else // If match
{
$ L_n + = $ k_len-1; // The Count skips the corresponding word count
$ Con. = "<span style =" color: # f00; font-weight: bold; text-decoration: underline "> ";
$ Con. = $ key;

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.