当前位置:Gxlcms > PHP教程 > 自动生成文章摘要的php代码

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

时间:2021-07-01 10:21:17 帮助过:13人阅读

  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) <= BRIEF_LENGTH ) return $text;
  6. $Foremost = substr($text, 0, BRIEF_LENGTH);
  7. $re = "/<(\/?)(P|DIV|H1|H2|H3|H4|H5|H6|ADDRESS|PRE|TABLE|TR|TD|TH|INPUT|SELECT|TEXTAREA|OBJECT|A|UL|OL|LI|BASE|META|LINK|HR|BR|PARAM|IMG|AREA|INPUT|SPAN)[^>]*(>?)/i";
  8. $Single = "/BASE|META|LINK|HR|BR|PARAM|IMG|AREA|INPUT/i";
  9. $Stack = array(); $posStack = array();
  10. preg_match_all($re,$Foremost,$matches, PREG_SET_ORDER | PREG_OFFSET_CAPTURE);
  11. /* [Child-matching Specification]:
  12. $matches[$i][1] : A "/" charactor indicating whether current "<...>" Friction is Closing Part
  13. $matches[$i][2] : Element Name.
  14. $matches[$i][3] : Right > of a "<...>" Friction */
  15. for($i = 0 ; $i < count($matches); $i++){
  16. if($matches[$i][1][0] == ""){
  17. $Elem = $matches[$i][2][0];
  18. if(preg_match($Single,$Elem) && $matches[$i][3][0] !=""){
  19. continue;
  20. }
  21. array_push($Stack, strtoupper($matches[$i][2][0]));
  22. array_push($posStack, $matches[$i][2][1]);
  23. if($matches[$i][3][0] =="") break;
  24. }else{
  25. $StackTop = $Stack[count($Stack)-1];
  26. $End = strtoupper($matches[$i][2][0]);
  27. if(strcasecmp($StackTop,$End)==0){
  28. array_pop($Stack);
  29. array_pop($posStack);
  30. if($matches[$i][3][0] ==""){
  31. $Foremost = $Foremost.">";
  32. }
  33. }
  34. }
  35. }
  36. $cutpos = array_shift($posStack) - 1;
  37. $Foremost = substr($Foremost,0,$cutpos);
  38. return $Foremost;
  39. };
  40. 若遇到函数对多字节字符集支持得不好的情况,大家可以参考下下面这个代码。

  41. 代码2:

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

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

人气教程排行