News.php 3.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107
  1. <?php
  2. namespace Content\Lib;
  3. use Dever;
  4. class News
  5. {
  6. # 根据文章id 获取文章信息
  7. public function get($data, $uid = false)
  8. {
  9. if (!is_array($data)) {
  10. $data = Dever::db('content/news')->find($data);
  11. }
  12. if (!$data) {
  13. Dever::alert('错误的专题信息');
  14. }
  15. //$data = $this->getContent($data, $uid);
  16. return $data;
  17. }
  18. public function getContent($data, $uid = false)
  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'], 'jpg')) {
  26. # gif
  27. preg_match_all('/<img(.*?)src="(.*?)\.jpg" (.*?)\/>/', $content, $matches);
  28. if (isset($matches[2])) {
  29. foreach ($matches[2] as $k => $v) {
  30. $content = str_replace($matches[0][$k], '{replace}'.count($replace).'{replace}', $content);
  31. $pic = $v;
  32. $replace[] = array('type' => 2, 'pic' => $pic . '.jpg');
  33. }
  34. }
  35. }
  36. elseif (strstr($data['content'], 'gif')) {
  37. # gif
  38. preg_match_all('/<img(.*?)src="(.*?)\.gif" (.*?)\/>/', $content, $matches);
  39. if (isset($matches[2])) {
  40. foreach ($matches[2] as $k => $v) {
  41. $content = str_replace($matches[0][$k], '{replace}'.count($replace).'{replace}', $content);
  42. $pic = $v;
  43. $replace[] = array('type' => 2, 'pic' => $pic . '.gif');
  44. }
  45. }
  46. }
  47. elseif (strstr($data['content'], 'png')) {
  48. # gif
  49. preg_match_all('/<img(.*?)src="(.*?)\.png" (.*?)\/>/', $content, $matches);
  50. if (isset($matches[2])) {
  51. foreach ($matches[2] as $k => $v) {
  52. $content = str_replace($matches[0][$k], '{replace}'.count($replace).'{replace}', $content);
  53. $pic = $v;
  54. $replace[] = array('type' => 2, 'pic' => $pic . '.png');
  55. }
  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. if (!$data['content_array']) {
  75. $data['content_array'][] = array
  76. (
  77. 'type' => 1,
  78. 'content' => $data['content'],
  79. );
  80. }
  81. unset($data['content']);
  82. return $data;
  83. }
  84. }