PHP implements keyword substitution with highlighted code

Source: Internet
Author: User
  1. /*

  2. Useful: Highlight Keywords
  3. Requirements: All < and > symbols in the reference article except HTML tags are replaced with < and > respectively
  4. $rows [' Content ']=str_replace ("<", "<", $rows [content]);
  5. $rows [' Content ']=str_replace (">", ">", $rows [content]);
  6. There may be a problem: not high efficiency forgot the case conversion problem
  7. $content: A reference article to highlight
  8. $key: Keywords
  9. site:bbs.it-home.org
  10. */
  11. function Highlight ($content, $key) {
  12. $k _fi=substr ($key, 0, 1); Get the first character of the keyword
  13. $k _len=strlen ($key); Calculate keywords Word count
  14. $l _len=strlen ($content); Count the number of articles in a count
  15. for ($l _n=0; $l _n< $l _len; $l _n++)//start the cycle according to the count
  16. {
  17. $l _s=substr ($content, $l _n,1); Get the current character of the article
  18. if ($l _s== "<")//If this character is the beginning of the label
  19. {
  20. while ($l _s!= ">")//We're looking for this label to close
  21. {
  22. $con. = $l _s; Import Results
  23. $l _n++; Of course, start taking the next character in the article.
  24. $l _s=substr ($content, $l _n,1);
  25. }
  26. $con. = $l _s;
  27. }
  28. ElseIf ($l _s== $k _fi)//If this character is the same as the first character of the keyword
  29. {
  30. $l _key=substr ($content, $l _n, $k _len); Check whether the current position of the article matches the keyword
  31. if ($l _key!= $key)
  32. {
  33. $con. = $l _s; Import Results
  34. }
  35. else//if matching
  36. {
  37. $l _n+= $k _len-1; Count skips the corresponding number of words
  38. $con. = "{$key}"; Highlight Keywords
  39. }
  40. }
  41. Else
  42. {
  43. $con. = $l _s; Import Results
  44. }
  45. }
  46. return $con;
  47. }

  48. Chinese test

  49. Reference string
  50. $STR = "Programmer's Home Programmer's home Programmer's house our big Programmer's home is a programmer's house";
  51. Keywords
  52. $key = "Home of the programmer";

  53. Calling functions

  54. $str _hl=highlight ($str, $key);

  55. echo "

    " . $str _hl. "

    \ n ";
  56. echo "";
  57. echo "

    " . Htmlspecialchars ($str _hl). "

    \ n ";
  58. ?>

Copy Code
  • 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.