Article.php 4.8 KB

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