欢迎您访问程序员文章站本站旨在为大家提供分享程序员计算机编程知识!
您现在的位置是: 首页  >  后端开发

自动生成文章摘要的php代码

程序员文章站 2022-03-31 13:16:45
...
  1. // PHP 4.3 or above needed

  2. define("BRIEF_LENGTH", 800); //Word amount of the Briefing of an Article
  3. function Generate_Brief($text){
  4. global $Briefing_Length;
  5. if(strlen($text) $Foremost = substr($text, 0, BRIEF_LENGTH);
  6. $re = "/]*(>?)/i";
  7. $Single = "/BASE|META|LINK|HR|BR|PARAM|IMG|AREA|INPUT/i";
  8. $Stack = array(); $posStack = array();
  9. preg_match_all($re,$Foremost,$matches, PREG_SET_ORDER | PREG_OFFSET_CAPTURE);
  10. /* [Child-matching Specification]:
  11. $matches[$i][1] : A "/" charactor indicating whether current "<...>" Friction is Closing Part
  12. $matches[$i][2] : Element Name.
  13. $matches[$i][3] : Right > of a "<...>" Friction */
  14. for($i = 0 ; $i if($matches[$i][1][0] == ""){
  15. $Elem = $matches[$i][2][0];
  16. if(preg_match($Single,$Elem) && $matches[$i][3][0] !=""){
  17. continue;
  18. }
  19. array_push($Stack, strtoupper($matches[$i][2][0]));
  20. array_push($posStack, $matches[$i][2][1]);
  21. if($matches[$i][3][0] =="") break;
  22. }else{
  23. $StackTop = $Stack[count($Stack)-1];
  24. $End = strtoupper($matches[$i][2][0]);
  25. if(strcasecmp($StackTop,$End)==0){
  26. array_pop($Stack);
  27. array_pop($posStack);
  28. if($matches[$i][3][0] ==""){
  29. $Foremost = $Foremost.">";
  30. }
  31. }
  32. }
  33. }
  34. $cutpos = array_shift($posStack) - 1;
  35. $Foremost = substr($Foremost,0,$cutpos);
  36. return $Foremost;
  37. };
  38. 若遇到函数对多字节字符集支持得不好的情况,大家可以参考下下面这个代码。
  39. 代码2:

  40. function Generate_Brief($text){
  41. global $Briefing_Length;
  42. mb_regex_encoding("UTF-8");
  43. if(mb_strlen($text) $Foremost = mb_substr($text, 0, BRIEF_LENGTH);
  44. $re = "]*(>?)";
  45. $Single = "/BASE|META|LINK|HR|BR|PARAM|IMG|AREA|INPUT|BR/i";
  46. $Stack = array(); $posStack = array();
  47. mb_ereg_search_init($Foremost, $re, 'i');
  48. while($pos = mb_ereg_search_pos()){
  49. $match = mb_ereg_search_getregs();
  50. /* [Child-matching Formulation]:
  51. $matche[1] : A "/" charactor indicating whether current "<...>" Friction is Closing Part
  52. $matche[2] : Element Name.
  53. $matche[3] : Right > of a "<...>" Friction
  54. */
  55. if($match[1]==""){
  56. $Elem = $match[2];
  57. if(mb_eregi($Single, $Elem) && $match[3] !=""){
  58. continue;
  59. }
  60. array_push($Stack, mb_strtoupper($Elem));
  61. array_push($posStack, $pos[0]);
  62. }else{
  63. $StackTop = $Stack[count($Stack)-1];
  64. $End = mb_strtoupper($match[2]);
  65. if(strcasecmp($StackTop,$End)==0){
  66. array_pop($Stack);
  67. array_pop($posStack);
  68. if($match[3] ==""){
  69. $Foremost = $Foremost.">";
  70. }
  71. }
  72. }
  73. }
  74. $cutpos = array_shift($posStack) - 1;
  75. $Foremost = mb_substr($Foremost,0,$cutpos,"UTF-8");
  76. return $Foremost;
  77. };
  78. ?>
复制代码

至此,自动生成文章摘要的php代码的两种方法都介绍完了,希望对您有所帮助。 编辑推荐: php 摘要生成函数(自定义,无乱码)