<? 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;