Article.php 3.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119
  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. $data['content'] = Dever::filter($data['content']);
  23. $content = $data['content'];
  24. $replace = array();
  25. if (strstr($data['content'], 'embed')) {
  26. # 音频
  27. preg_match_all('/<p(.*?)>([\s]+)<embed src="(.*?)"(.*?)\/>([\s]+)<\/p>/i', $data['content'], $matches);
  28. if (isset($matches[3])) {
  29. foreach ($matches[3] as $k => $v) {
  30. $content = str_replace($matches[0][$k], '{replace}'.count($replace).'{replace}', $content);
  31. $replace[] = array('type' => 5, 'content' => $v);
  32. }
  33. }
  34. }
  35. if (strstr($data['content'], 'data-id')) {
  36. # 视频
  37. preg_match_all('/<p(.*?)>([\s]+)<img(.*?)data-id="(.*?)" data-key="(.*?)" \/>([\s]+)<\/p>/', $data['content'], $matches);
  38. if (!isset($matches[4][0])) {
  39. preg_match_all('/<p(.*?)>([\s]+)<img(.*?)data-key="(.*?)" data-id="(.*?)" \/>([\s]+)<\/p>/', $data['content'], $matches);
  40. }
  41. if (isset($matches[4][0]) && isset($matches[5][0])) {
  42. foreach ($matches[4] as $k => $v) {
  43. $content = str_replace($matches[0][$k], '{replace}'.count($replace).'{replace}', $content);
  44. if ($matches[5][$k] == 'video/lib/core.vod') {
  45. $method = 'video/lib/vod';
  46. $type = 2;
  47. } else {
  48. $type = 3;
  49. $method = 'video/lib/live';
  50. }
  51. $info = Dever::load($method)->get($v);
  52. if (isset($info['content'])) {
  53. unset($info['content']);
  54. }
  55. $replace[] = array('id' => $v, 'type' => $type, 'content' => $info);
  56. }
  57. }
  58. //$data['content'] = preg_replace('/<div class="dever-drop">([\s\S]*?)<\/div>/i', '', $data['content']);
  59. $content = explode('{replace}', $content);
  60. $data['content_array'] = array();
  61. //print_r($content);die;
  62. foreach ($content as $k => $v) {
  63. $v = trim($v);
  64. if (is_numeric($v) && $v >= 0 && isset($replace[$v])) {
  65. $data['content_array'][] = $replace[$v];
  66. } elseif ($v) {
  67. $data['content_array'][] = array
  68. (
  69. 'type' => 1,
  70. 'content' => $v,
  71. );
  72. }
  73. }
  74. }
  75. if (!$data['content_array']) {
  76. $data['content_array'][] = array
  77. (
  78. 'type' => 1,
  79. 'content' => $data['content'],
  80. );
  81. }
  82. unset($data['content']);
  83. return $data;
  84. }
  85. # 获取相关推荐
  86. public function getRelation($info)
  87. {
  88. $where['noid'] = $info['id'];
  89. $where['cate_id'] = $info['cate_id'];
  90. return Dever::db('content/article')->getRelation($where);
  91. }
  92. # 增加浏览量
  93. public function addView($id)
  94. {
  95. Dever::db('content/article')->addView($id);
  96. }
  97. }