Automatically add the span tag and keyword highlighting code for php keywords

Source: Internet
Author: User
Automatically add the span tag and keyword highlighting code for php keywords

  1. // Example use: $ spanned = codeWords ($ string_containing_keywords );
  2. // My site: andrew. dx. am
  3. // Using color = blue, but different arrays of words and different
  4. // Colours can be added.
  5. Function onlyWholeWords (& $ value, $ key ){
  6. // Ignores words after // comment delimiters.
  7. // $ Value = "/\ B (". $ value. ") \ B/"; // doesn't handle comments
  8. // $ Value = "/^ (? :(?! \/).) * \ K \ B (". $ value.") \ B /";
  9. // \ K lookbehind alternative is not supported in PHP <5.2.4, so use:
  10. $ Value = "/^ ((? :(?! \/).) *) \ B ". $ value." \ B /";
  11. }
  12. Function addSpan (& $ value, $ key, $ color = 'blue '){
  13. $ Value = "$1". $ value ."";
  14. }
  15. Function codeWords ($ code ){
  16. $ Keywords = array ('as', 'break', 'case', 'class ',
  17. 'Continue ', 'default', 'Do', 'elif', 'else ',
  18. 'Elseif', 'for', 'foreach', 'function', 'if ',
  19. 'New', 'null', 'Return ', 'self', 'Switch ',
  20. 'This', 'to', 'typeof ', 'Until ',
  21. 'Var', 'void', 'wait', 'with ');
  22. $ Keywords2 = $ keywords;
  23. Array_walk ($ keywords, 'onlywholeword ');
  24. Array_walk ($ keywords2, 'addspan ', 'blue ');
  25. $ Code = preg_replace ($ keywords, $ keywords2, $ code );
  26. Return $ code;
  27. }

II. php automatically adds a keyword link to the article

Php function code that automatically adds a keyword link to the article.

Code:

  1. $ Link = array (

  2. Baidu, http://www.baidu.com /',
  3. 'Php tutorial, http://bbs.it-home.org/wb/php /',
  4. 'Script School, http://bbs.it-home.org /',
  5. );
  6. $ Str = 'Search for php tutorials in Baidu to go to the php programming article provided by the programmer's house.
  7. The content to be replaced is placed here. ';
  8. $ Out = keylink ($ str, $ link, 1); // $ str original character $ link, replacing the link array, 3 replace times
  9. Echo $ out;
  10. Function _ sortDesc ($ a, $ B ){
  11. Return (strlen ($ a [0]) <strlen ($ B [0])? 1:-1;
  12. }
  13. Function keylink ($ str, $ link, $ count = 1)
  14. {
  15. $ LinkDefs = $ link;
  16. $ LinkMap = array ();
  17. Foreach ($ linkDefs as $ row ){
  18. $ LinkMap [] = explode (',', $ row );
  19. }

  20. Foreach ($ linkMap as $ row ){

  21. $ Str = preg_replace ('/(\ s *)('. $ row [0]. ') (\ s * <\/a>)/sui',' $ {2} ', $ str );
  22. }

  23. Usort ($ linkMap, '_ sortDesc ');

  24. $ TmpKwds = array ();

  25. Foreach ($ linkMap as $ I =>$ row ){

  26. List ($ kwd, $ url) = $ row;
  27. For ($ j = $ I + 1; $ j $ SubKwd = $ linkMap [$ j] [0];
  28. // If other keywords are included, replace them with other strings.
  29. If (strpos ($ kwd, $ subKwd )! = False ){
  30. $ TmpKwd = '{'. md5 ($ subKwd ).'}';
  31. $ Kwd = str_replace ($ subKwd, $ tmpKwd, $ kwd );
  32. $ TmpKwds [$ tmpKwd] = $ subKwd;
  33. }
  34. }
  35. // Replace text with link
  36. $ Str = preg_replace ('/('. $ row [0]. ')/sui','. $ kwd. '', $ str, $ count );
  37. }

  38. // Replace the string that replaces the subkeyword

  39. Foreach ($ tmpKwds as $ tmp => $ kwd ){
  40. $ Str = str_replace ($ tmp, $ kwd, $ str );
  41. }
  42. Return $ str;
  43. }
  44. ?>

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.