HTML Preview Regular Replace

Source: Internet
Author: User
Tags readfile rtrim
HTML Preview Regular Replace
  1. /**
  2. * HTML replacement processing class, consider the following several substitutions
  3. * 1. IMG src: '/]+?) /I '
  4. * 2. a href: '/]+? /I '
  5. * 3. IFRAM.SRC: '/ ]+?) /I '
  6. * 4. Frame src: '/ ]+?) /I '
  7. * 5. JS: '/window.open ([(]+?)] ([\'" ]+?) (.+?) ([ )+?]) /I '
  8. * 6. CSS: '/background (. +?) URL ([(]) ([\ ' "]+?) (.+?) ([ )+?]) /I '
  9. */
  10. Class Myreplace {
  11. Private $moudle _array = Array (' Udata ', ' tdata ', ' TResult ', ' dresult ');
  12. Private $content;
  13. Private $relative _dirname;
  14. Private $projectid;
  15. Private $moudle;
  16. function __construct () {
  17. $this->ci = &get_instance ();
  18. }
  19. /**
  20. * Replace
  21. * @param string $contentHTML content
  22. * @param string $relative relative path
  23. * @param int $projectid Project ID
  24. * @moudlestring $moudle template ID: Udata,tdata,tresult,dresult
  25. */
  26. Public Function My_replace ($content, $relative, $projectid, $moudle) {
  27. $this->content = $content;
  28. $this->relative_dirname = $relative;
  29. $this->projectid = $projectid;
  30. if (In_array (Strtolower ($moudle), $this->moudle_array))
  31. $this->moudle = $moudle;
  32. else exit;
  33. Switch ($this->moudle) {
  34. Case ' Udata ':
  35. $this->ci->load->model (' mupload_data ', ' model ');
  36. Break
  37. Case ' Tdata ':
  38. $this->ci->load->model (' taskdata ', ' model ');
  39. Break
  40. Case ' TResult ':
  41. $this->ci->load->model (' Taskresult ', ' model ');
  42. Break
  43. Case ' Dresult ':
  44. $this->ci->load->model (' Dmsresult ', ' model ');
  45. Break
  46. Default
  47. Break
  48. }
  49. $pattern = '/]+? ') /I ';
  50. $content = Preg_replace_callback ($pattern, Array ($this, ' image_replace '), $content);
  51. $pattern = '/]+? ') /I ';
  52. $content = Preg_replace_callback ($pattern, Array ($this, ' html_replace '), $content);
  53. $pattern = '/ ]+?) /I ';
  54. $content = Preg_replace_callback ($pattern, Array ($this, ' iframe_replace '), $content);
  55. $pattern = '/ ]+?) /I ';
  56. $content = Preg_replace_callback ($pattern, Array ($this, ' frame_replace '), $content);
  57. $pattern = '/window.open ([]+?) ([\'" ]+?) (.+?) ([ )]+?) /I ';
  58. $content = Preg_replace_callback ($pattern, Array ($this, ' js_replace '), $content);
  59. $pattern = '/background (. +?) URL ([(]) ([\ ' "]+?) (.+?) ([ )+?]) /I ';
  60. $content = Preg_replace_callback ($pattern, Array ($this, ' css_replace '), $content);
  61. return $content;
  62. }
  63. Private Function Image_replace ($matches) {
  64. if (count ($matches) < 4) return ';
  65. if (Empty ($matches [3])) return ';
  66. $matches [3] = RTrim ($matches [3], ' \ ' "/');
  67. Get the ID of a picture
  68. $parent _dir_num = Substr_count ($matches [3], ' ... /');
  69. $relative _dirname = $this->relative_dirname;
  70. for ($i =0; $i < $parent _dir_num; $i + +) {
  71. $relative _dirname = substr ($relative _dirname, 0, Strrpos ($relative _dirname, "/"));
  72. }
  73. $relativepath = RTrim ($relative _dirname, '/'). '/'. LTrim ($matches [3], './');
  74. $image _id = $this->ci->model->get_id_by_path_and_project ($relativepath, $this->projectid);
  75. Output
  76. if (!empty ($image _id)) {
  77. if ($this->moudle = = ' Dresult ') {
  78. Return "Ci->config->item (" Base_url ")." cdms/". $this->moudle." /readpic/$image _id?pid= ". $this->projectid. $matches [2]. $matches [4];
  79. } else {
  80. Return "Ci->config->item (" Base_url ")." cdms/". $this->moudle." /picfile/$image _id?pid= ". $this->projectid. $matches [2]. $matches [4];
  81. }
  82. } else {
  83. Return "}
  84. }
  85. Private Function Html_replace ($matches) {
  86. if (count ($matches) < 4) return ';
  87. if (Empty ($matches [3])) return ';
  88. If the href link ($matches [3]) starts with HTTP or www or mailto, it is not processed
  89. if (Preg_match ('/^[http|www|mailto] (. +?) /I ', $matches [3]))
  90. Return "$matches [3] = RTrim ($matches [3], ' \ '"/');
  91. Working with anchor points
  92. if (Substr_count ($matches [3], ' # ') >0)
  93. $matches [3] = substr ($matches [3],0,strrpos ($matches [3], ' # '));
  94. Gets the ID of the HTML
  95. $parent _dir_num = Substr_count ($matches [3], ' ... /');
  96. $relative _dirname = $this->relative_dirname;
  97. for ($i =0; $i < $parent _dir_num; $i + +) {
  98. $relative _dirname = substr ($relative _dirname, 0, Strrpos ($relative _dirname, "/"));
  99. }
  100. $relativepath = RTrim ($relative _dirname, '/'). '/'. LTrim ($matches [3], './');
  101. $txtfile _id = $this->ci->model->get_id_by_path_and_project ($relativepath, $this->projectid);
  102. Output
  103. if (!empty ($txtfile _id)) {
  104. if ($this->moudle = = ' Dresult ') {
  105. Return "Ci->config->item (" Base_url ")." cdms/". $this->moudle." /readfile/$txtfile _id?pid= ". $this->projectid. $matches [2]. $matches [4];
  106. } else {
  107. Return "Ci->config->item (" Base_url ")." cdms/". $this->moudle." /txtfile/$txtfile _id?pid= ". $this->projectid. $matches [2]. $matches [4];
  108. }
  109. } else {
  110. Return "}
  111. }
  112. Private Function Iframe_replace ($matches) {
  113. if (count ($matches) < 4) return ';
  114. if (Empty ($matches [3])) return ';
  115. $matches [3] = RTrim ($matches [3], ' \ ' "/');
  116. Working with anchor points
  117. if (Substr_count ($matches [3], ' # ') >0)
  118. $matches [3] = substr ($matches [3],0,strrpos ($matches [3], ' # '));
  119. Gets the ID of the HTML
  120. $parent _dir_num = Substr_count ($matches [3], ' ... /');
  121. $relative _dirname = $this->relative_dirname;
  122. for ($i =0; $i < $parent _dir_num; $i + +) {
  123. $relative _dirname = substr ($relative _dirname, 0, Strrpos ($relative _dirname, "/"));
  124. }
  125. $relativepath = RTrim ($relative _dirname, '/'). '/'. LTrim ($matches [3], './');
  126. $txtfile _id = $this->ci->model->get_id_by_path_and_project ($relativepath, $this->projectid);
  127. Output
  128. if (!empty ($txtfile _id)) {
  129. if ($this->moudle = = ' Dresult ') {
  130. Return " ci->config->item (" Base_url ")." cdms/". $this->moudle." /readfile/$txtfile _id?pid= ". $this->projectid. $matches [2]. $matches [4];
  131. } else {
  132. Return " ci->config->item (" Base_url ")." cdms/". $this->moudle." /txtfile/$txtfile _id?pid= ". $this->projectid. $matches [2]. $matches [4];
  133. }
  134. } else {
  135. Return "<>
  136. }
  137. }
  138. Private Function Frame_replace ($matches) {
  139. if (count ($matches) < 4) return ';
  140. if (Empty ($matches [3])) return ';
  141. $matches [3] = RTrim ($matches [3], ' \ ' "/');
  142. Working with anchor points
  143. if (Substr_count ($matches [3], ' # ') >0)
  144. $matches [3] = substr ($matches [3],0,strrpos ($matches [3], ' # '));
  145. Gets the ID of the HTML
  146. $parent _dir_num = Substr_count ($matches [3], ' ... /');
  147. $relative _dirname = $this->relative_dirname;
  148. for ($i =0; $i < $parent _dir_num; $i + +) {
  149. $relative _dirname = substr ($relative _dirname, 0, Strrpos ($relative _dirname, "/"));
  150. }
  151. $relativepath = RTrim ($relative _dirname, '/'). '/'. LTrim ($matches [3], './');
  152. $txtfile _id = $this->ci->model->get_id_by_path_and_project ($relativepath, $this->projectid);
  153. Output
  154. if (!empty ($txtfile _id)) {
  155. if ($this->moudle = = ' Dresult ') {
  156. Return " ci->config->item (" Base_url ")." cdms/". $this->moudle." /readfile/$txtfile _id?pid= ". $this->projectid. $matches [2]. $matches [4];
  157. } else {
  158. Return " ci->config->item (" Base_url ")." cdms/". $this->moudle." /txtfile/$txtfile _id?pid= ". $this->projectid. $matches [2]. $matches [4];
  159. }
  160. } else {
  161. Return "<>
  162. }
  163. }
  164. Private Function Js_replace ($matches) {
  165. if (count ($matches) < 4) return ';
  166. if (Empty ($matches [3])) return ';
  167. Working with links
  168. $arr _html = Split (', ', $matches [3]);
  169. $href = $arr _html[0];
  170. $other = ";
  171. for ($i =0; $i
  172. $other = $arr _html[$i]. ",";
  173. $other = RTrim ($other, "\,");
  174. $href =rtrim ($href, ' \ ' \ ');
  175. Working with anchor points
  176. if (Substr_count ($href, ' # ') >0)
  177. Return "window.open". $matches [1]. $matches [2]. $matches [3]. $matches [4];
  178. Gets the ID of the HTML
  179. $parent _dir_num = Substr_count ($href, ' ... /');
  180. $relative _dirname = $this->relative_dirname;
  181. for ($i =0; $i < $parent _dir_num; $i + +) {
  182. $relative _dirname = substr ($relative _dirname, 0, Strrpos ($relative _dirname, "/"));
  183. }
  184. $relativepath = RTrim ($relative _dirname, '/'). '/'. LTrim ($href, './');
  185. $txtfile _id = $this->ci->model->get_id_by_path_and_project ($relativepath, $this->projectid);
  186. Output
  187. if (!empty ($txtfile _id)) {
  188. if ($this->moudle = = ' Dresult ') {
  189. Return "window.open". $matches [1]. $matches [2]. $this->ci->config->item ("Base_url"). " cdms/". $this->moudle." /readfile/$txtfile _id?pid= ". $this->projectid. $matches [2]. ', '. $other. $matches [4];
  190. } else {
  191. Return "window.open". $matches [1]. $matches [2]. $this->ci->config->item ("Base_url"). " cdms/". $this->moudle." /txtfile/$txtfile _id?pid= ". $this->projectid. $matches [2]. ', '. $other. $matches [4];
  192. }
  193. } else {
  194. Return "window.open". $matches [1]. $matches [2]. $matches [3]. $matches [4];
  195. }
  196. }
  197. Private Function Css_replace ($matches) {
  198. if (count ($matches) < 5) return ';
  199. if (Empty ($matches [4])) return ';
  200. $matches [4] = RTrim ($matches [4], ' \ ' "/');
  201. Get the ID of a picture
  202. $parent _dir_num = Substr_count ($matches [4], ' ... /');
  203. $relative _dirname = $this->relative_dirname;
  204. for ($i =0; $i < $parent _dir_num; $i + +) {
  205. $relative _dirname = substr ($relative _dirname, 0, Strrpos ($relative _dirname, "/"));
  206. }
  207. $relativepath = RTrim ($relative _dirname, '/'). '/'. LTrim ($matches [4], './');
  208. $image _id = $this->ci->model->get_id_by_path_and_project ($relativepath, $this->projectid);
  209. Output
  210. if (!empty ($image _id)) {
  211. if ($this->moudle = = ' Dresult ') {
  212. Return "Background". $matches [1]. " URL ". $matches [2]. $matches [3]. $this->ci->config->item (" Base_url ")." cdms/". $this->moudle." /readpic/$image _id?pid= ". $this->projectid. $matches [3]. $matches [5];
  213. } else {
  214. Return "Background". $matches [1]. " URL ". $matches [2]. $matches [3]. $this->ci->config->item (" Base_url ")." cdms/". $this->moudle." /picfile/$image _id?pid= ". $this->projectid. $matches [3]. $matches [5];
  215. }
  216. } else {
  217. Return "Background". $matches [1]. " URL ". $matches [2]. $matches [3]. $matches [4]. $matches [3]. $matches [5];
  218. }
  219. }
  220. }
  221. /* End of myreplace.php */
  222. /* Location:/application/libraries/myreplace.php */
Copy Code
  • Related Article

    Contact Us

    The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

    If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

    A Free Trial That Lets You Build Big!

    Start building with 50+ products and up to 12 months usage for Elastic Compute Service

    • Sales Support

      1 on 1 presale consultation

    • After-Sales Support

      24/7 Technical Support 6 Free Tickets per Quarter Faster Response

    • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.