Article.php 4.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131
  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]) && isset($matches[5][0])) {
  52. foreach ($matches[4] as $k => $v) {
  53. $content = str_replace($matches[0][$k], '{replace}'.count($replace).'{replace}', $content);
  54. if ($matches[5][$k] == 'video/lib/core.vod') {
  55. $method = 'video/lib/vod';
  56. $type = 2;
  57. } else {
  58. $type = 3;
  59. $method = 'video/lib/live';
  60. }
  61. $info = Dever::load($method)->get($v);
  62. if (isset($info['content'])) {
  63. unset($info['content']);
  64. }
  65. $replace[] = array('id' => $v, 'type' => $type, 'content' => $info);
  66. }
  67. }
  68. //$data['content'] = preg_replace('/<div class="dever-drop">([\s\S]*?)<\/div>/i', '', $data['content']);
  69. $content = explode('{replace}', $content);
  70. $data['content_array'] = array();
  71. //print_r($content);die;
  72. foreach ($content as $k => $v) {
  73. $v = trim($v);
  74. if (is_numeric($v) && $v >= 0 && isset($replace[$v])) {
  75. $data['content_array'][] = $replace[$v];
  76. } elseif ($v) {
  77. $data['content_array'][] = array
  78. (
  79. 'type' => 1,
  80. 'content' => $v,
  81. );
  82. }
  83. }
  84. }
  85. if (!$data['content_array']) {
  86. $data['content_array'][] = array
  87. (
  88. 'type' => 1,
  89. 'content' => $data['content'],
  90. );
  91. }
  92. unset($data['content']);
  93. return $data;
  94. }
  95. # 获取相关推荐
  96. public function getRelation($info)
  97. {
  98. $where['noid'] = $info['id'];
  99. $where['cate_id'] = $info['cate_id'];
  100. return Dever::db('content/article')->getRelation($where);
  101. }
  102. # 增加浏览量
  103. public function addView($id)
  104. {
  105. Dever::db('content/article')->addView($id);
  106. }
  107. }