? Php * usage: highlighted keyword requirements: For future reference, use lt; and gt for all and symbols except HTML tags in the document to replace $ rows [content] str_replace (, lt ;, $ rows [content]); $ rows [content] str_replace (, gt;, $ rows [content]); possible problems: inefficient, case-insensitive conversion $ content: add
/*
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 ";
?>