Use PHP to highlight perfect keywords

Source: Internet
Author: User
? Php/* usage: highlighted keyword requirements: For future reference, use lt; and gt for all and symbols except HTML tags in the document; replace $ rows [content] str_replace (, lt ;, $ rows [content]); $ rows [content] str_replace (, gt;, $ rows [content]); possible problems: inefficient and case-insensitive conversions


/*
Usage: highlight keywords
Requirements: all <and> Symbols except HTML tags in the document are replaced by <and>.
$ Rows ['content'] = str_replace ("<", "<", $ rows [content]);
$ Rows ['content'] = str_replace (">", ">", $ 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. = "";
$ Con. = $ key;
$ Con. = ""; // highlight keywords
}
}
Else
{
$ Con. = $ l_s; // import result
}
}
Return $ con;
}

#####################
// Perform the following English test
#####################

// String for future reference
$ Str = "aabbccdd Aabbccddaabbccdd ";

// Keyword
$ Key = "bc ";

// Call a function
$ Str_hl = highlight ($ str, $ key );

// Print the output to the screen
Echo "###################### \ n ";
Echo "test \ n" in English below ";
Echo "###################### \ n ";
Echo"

Original string

\ N ";
Echo"

$ Str

\ N ";
Echo"

". Htmlspecialchars ($ str )."

\ N ";
Echo"

Keyword: $ key

\ N ";
Echo"

String after brightening

\ N ";
Echo"

$ Str_hl

\ N ";
Echo"

". Htmlspecialchars ($ str_hl )."

\ N ";

#####################
// Perform the following Chinese test
#####################

// String for future reference
$ Str = "hehahahahahahaiku Hehahahai lakuo Hehahahai la cool ";

// Keyword
$ Key = "Haoxi ";

// Call a function
$ Str_hl = highlight ($ str, $ key );

// Print the output to the screen
Echo "###################### \ n ";
Echo "the following is a Chinese test \ n ";
Echo "###################### \ n ";
Echo"

Original string

\ N ";
Echo"

$ Str

\ N ";
Echo"

". Htmlspecialchars ($ str )."

\ N ";
Echo"

Keyword: $ key

\ N ";
Echo"

String after brightening

\ N ";
Echo"

$ Str_hl

\ N ";
Echo"

". Htmlspecialchars ($ str_hl )."

\ N ";


?>

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.