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

php中文网
发布: 2016-07-25 09:04:04
原创
1346人浏览过
  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:

    码上飞
    码上飞

    码上飞(CodeFlying) 是一款AI自动化开发平台,通过自然语言描述即可自动生成完整应用程序。

    码上飞 138
    查看详情 码上飞
  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速学教程(入门到精通)
PHP速学教程(入门到精通)

PHP怎么学习?PHP怎么入门?PHP在哪学?PHP怎么学才快?不用担心,这里为大家提供了PHP速学教程(入门到精通),有需要的小伙伴保存下载就能学习啦!

下载
来源:php中文网
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系admin@php.cn
最新问题
开源免费商场系统广告
热门教程
更多>
最新下载
更多>
网站特效
网站源码
网站素材
前端模板
关于我们 免责申明 举报中心 意见反馈 讲师合作 广告合作 最新更新 English
php中文网:公益在线php培训,帮助PHP学习者快速成长!
关注服务号 技术交流群
PHP中文网订阅号
每天精选资源文章推送
PHP中文网APP
随时随地碎片化学习

Copyright 2014-2025 https://www.php.cn/ All Rights Reserved | php.cn | 湘ICP备2023035733号