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

WBOY
Freigeben: 2016-07-25 09:04:04
Original
1048 Leute haben es durchsucht
  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 摘要生成函数(自定义,无乱码)



Verwandte Etiketten:
Quelle:php.cn
Erklärung dieser Website
Der Inhalt dieses Artikels wird freiwillig von Internetnutzern beigesteuert und das Urheberrecht liegt beim ursprünglichen Autor. Diese Website übernimmt keine entsprechende rechtliche Verantwortung. Wenn Sie Inhalte finden, bei denen der Verdacht eines Plagiats oder einer Rechtsverletzung besteht, wenden Sie sich bitte an admin@php.cn
Beliebte Tutorials
Mehr>
Neueste Downloads
Mehr>
Web-Effekte
Quellcode der Website
Website-Materialien
Frontend-Vorlage
Über uns Haftungsausschluss Sitemap
Chinesische PHP-Website:Online-PHP-Schulung für das Gemeinwohl,Helfen Sie PHP-Lernenden, sich schnell weiterzuentwickeln!