Article.php 4.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142
  1. <?php
  2. namespace Content\Lib;
  3. use Dever;
  4. class Article
  5. {
  6. # 根据文章id 获取文章信息
  7. public function get($data)
  8. {
  9. if (!is_array($data)) {
  10. $data = Dever::db('content/article')->getOne($data);
  11. }
  12. if (!$data) {
  13. Dever::alert('错误的文章信息');
  14. }
  15. $data = $this->getContent($data);
  16. return $data;
  17. }
  18. public function getContent($data)
  19. {
  20. //embed
  21. $data['content_array'] = array();
  22. if (strstr($data['content'], 'font-family:" hiragino="" sans="" gb","microsoft="" yahei","simsun";')) {
  23. $data['content'] = str_replace('font-family:" hiragino="" sans="" gb","microsoft="" yahei","simsun";', '', $data['content']);
  24. }
  25. if (strstr($data['content'], 'font-family:" gb","microsoft="" sans="" hiragino="" yahei","simsun";')) {
  26. $data['content'] = str_replace('font-family:" gb","microsoft="" sans="" hiragino="" yahei","simsun";', '', $data['content']);
  27. }
  28. if (strstr($data['content'], 'font-family:" yahei","simsun";')) {
  29. $data['content'] = str_replace('font-family:" yahei","simsun";', '', $data['content']);
  30. }
  31. if (strstr($data['content'], '"="" gb","microsoft="" sans="" hiragino="')) {
  32. $data['content'] = str_replace('"="" gb","microsoft="" sans="" hiragino="', '', $data['content']);
  33. }
  34. if (strstr($data['content'], 'font-family:" microsoft="" yahei","黑体",sans-serif')) {
  35. $data['content'] = str_replace('font-family:" microsoft="" yahei","黑体",sans-serif', '', $data['content']);
  36. }
  37. if (strstr($data['content'], '"="')) {
  38. $data['content'] = str_replace('"="', '', $data['content']);
  39. }
  40. $content = $data['content'];
  41. $replace = array();
  42. if (strstr($data['content'], 'embed')) {
  43. //print_r($data['content']);die;
  44. # 音频
  45. preg_match_all('/<p(.*?)>([\s]+)<embed src="(.*?)"(.*?)\/>([\s]+)<\/p>/i', $data['content'], $matches);
  46. if (isset($matches[3])) {
  47. foreach ($matches[3] as $k => $v) {
  48. $content = str_replace($matches[0][$k], '{replace}'.count($replace).'{replace}', $content);
  49. $replace[] = array('type' => 5, 'content' => $v);
  50. }
  51. }
  52. }
  53. if (strstr($data['content'], 'data-id')) {
  54. # 视频
  55. preg_match_all('/<p(.*?)>([\s]+)<img(.*?)data-id="(.*?)" data-key="(.*?)" \/>([\s]+)<\/p>/', $data['content'], $matches);
  56. if (!isset($matches[4][0])) {
  57. preg_match_all('/<p(.*?)>([\s]+)<img(.*?)data-key="(.*?)" data-id="(.*?)" \/>([\s]+)<\/p>/', $data['content'], $matches);
  58. }
  59. if (isset($matches[4][0]) && isset($matches[5][0])) {
  60. foreach ($matches[4] as $k => $v) {
  61. $content = str_replace($matches[0][$k], '{replace}'.count($replace).'{replace}', $content);
  62. if ($matches[5][$k] == 'video/lib/core.vod') {
  63. $method = 'video/lib/vod';
  64. $type = 2;
  65. } else {
  66. $type = 3;
  67. $method = 'video/lib/live';
  68. }
  69. $info = Dever::load($method)->get($v);
  70. if (isset($info['content'])) {
  71. unset($info['content']);
  72. }
  73. $replace[] = array('id' => $v, 'type' => $type, 'content' => $info);
  74. }
  75. }
  76. //$data['content'] = preg_replace('/<div class="dever-drop">([\s\S]*?)<\/div>/i', '', $data['content']);
  77. $content = explode('{replace}', $content);
  78. $data['content_array'] = array();
  79. //print_r($content);die;
  80. foreach ($content as $k => $v) {
  81. $v = trim($v);
  82. if (is_numeric($v) && $v >= 0 && isset($replace[$v])) {
  83. $data['content_array'][] = $replace[$v];
  84. } elseif ($v) {
  85. $data['content_array'][] = array
  86. (
  87. 'type' => 1,
  88. 'content' => $v,
  89. );
  90. }
  91. }
  92. }
  93. if (!$data['content_array']) {
  94. $data['content_array'][] = array
  95. (
  96. 'type' => 1,
  97. 'content' => $data['content'],
  98. );
  99. }
  100. unset($data['content']);
  101. return $data;
  102. }
  103. # 获取相关推荐
  104. public function getRelation($info)
  105. {
  106. $where['noid'] = $info['id'];
  107. $where['cate_id'] = $info['cate_id'];
  108. return Dever::db('content/article')->getRelation($where);
  109. }
  110. # 增加浏览量
  111. public function addView($id)
  112. {
  113. Dever::db('content/article')->addView($id);
  114. }
  115. }