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

WBOY
풀어 주다: 2016-07-25 09:04:04
원래의
1047명이 탐색했습니다.
  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 摘要生成函数(自定义,无乱码)



원천:php.cn
본 웹사이트의 성명
본 글의 내용은 네티즌들의 자발적인 기여로 작성되었으며, 저작권은 원저작자에게 있습니다. 본 사이트는 이에 상응하는 법적 책임을 지지 않습니다. 표절이나 침해가 의심되는 콘텐츠를 발견한 경우 admin@php.cn으로 문의하세요.
최신 이슈
인기 튜토리얼
더>
최신 다운로드
더>
웹 효과
웹사이트 소스 코드
웹사이트 자료
프론트엔드 템플릿
회사 소개 부인 성명 Sitemap
PHP 중국어 웹사이트:공공복지 온라인 PHP 교육,PHP 학습자의 빠른 성장을 도와주세요!