当前位置:Gxlcms > PHP教程 > HTML预览正则替换

HTML预览正则替换

时间:2021-07-01 10:21:17 帮助过:8人阅读

HTML预览 正则替换
  1. /**
  2. * HTML替换处理类,考虑如下几种替换
  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. * 替换
  21. * @param string $content HTML内容
  22. * @param string $relative 相对路径
  23. * @param int $projectid 项目id
  24. * @moudle string $moudle 模板标识: 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. //获取图片的id
  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. //输出
  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. //如果href的链接($matches[3])以http或www或mailto开始,则不进行处理
  89. //if(preg_match('/^[http|www|mailto](.+?)/i',$matches[3]))
  90. // return " $matches[3] = rtrim($matches[3],'\'"/');
  91. //处理锚点
  92. if(substr_count($matches[3],'#')>0)
  93. $matches[3] = substr($matches[3],0,strrpos($matches[3],'#'));
  94. //获取html的id
  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. //输出
  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. //处理锚点
  117. if(substr_count($matches[3],'#')>0)
  118. $matches[3] = substr($matches[3],0,strrpos($matches[3],'#'));
  119. //获取html的id
  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. //输出
  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. private function frame_replace( $matches ) {
  138. if(count($matches) < 4) return '';
  139. if( empty($matches[3]) ) return '';
  140. $matches[3] = rtrim($matches[3],'\'"/');
  141. //处理锚点
  142. if(substr_count($matches[3],'#')>0)
  143. $matches[3] = substr($matches[3],0,strrpos($matches[3],'#'));
  144. //获取html的id
  145. $parent_dir_num = substr_count( $matches[3], '../');
  146. $relative_dirname = $this->relative_dirname;
  147. for($i=0; $i<$parent_dir_num; $i++) {
  148. $relative_dirname = substr( $relative_dirname, 0, strrpos($relative_dirname,"/") );
  149. }
  150. $relativepath = rtrim($relative_dirname,'/') . '/'.ltrim($matches[3],'./');
  151. $txtfile_id = $this->CI->model->get_id_by_path_and_project($relativepath,$this->projectid);
  152. //输出
  153. if( !empty($txtfile_id ) ) {
  154. if($this->moudle == 'dresult') {
  155. return "CI->config->item("base_url")."cdms/".$this->moudle."/readfile/$txtfile_id?pid=".$this->projectid.$matches[2].$matches[4];
  156. } else {
  157. return "CI->config->item("base_url")."cdms/".$this->moudle."/txtfile/$txtfile_id?pid=".$this->projectid.$matches[2].$matches[4];
  158. }
  159. } else {
  160. return " }
  161. }
  162. private function js_replace( $matches ){
  163. if(count($matches) < 4) return '';
  164. if( empty($matches[3]) ) return '';
  165. //处理链接
  166. $arr_html = split(',',$matches[3]);
  167. $href = $arr_html[0];
  168. $other = '';
  169. for($i=0; $i $other = $arr_html[$i].", ";
  170. $other = rtrim($other,"\, ");
  171. $href =rtrim($href,'\'\"');
  172. //处理锚点
  173. if(substr_count($href,'#')>0)
  174. return "window.open".$matches[1].$matches[2].$matches[3].$matches[4];;
  175. //获取html的id
  176. $parent_dir_num = substr_count( $href, '../');
  177. $relative_dirname = $this->relative_dirname;
  178. for($i=0; $i<$parent_dir_num; $i++) {
  179. $relative_dirname = substr( $relative_dirname, 0, strrpos($relative_dirname,"/") );
  180. }
  181. $relativepath = rtrim($relative_dirname,'/') . '/'.ltrim($href,'./');
  182. $txtfile_id = $this->CI->model->get_id_by_path_and_project($relativepath,$this->projectid);
  183. //输出
  184. if( !empty($txtfile_id ) ) {
  185. if($this->moudle == 'dresult') {
  186. return "window.open".$matches[1]https://img.gxlcms.com/https://img.gxlcms.com/.$matches[2].$this->CI->config->item("base_url")."cdms/".$this->moudle."/readfile/$txtfile_id?pid=".$this->projectid.$matches[2].','.$other.$matches[4];
  187. } else {
  188. return "window.open".$matches[1]https://img.gxlcms.com/https://img.gxlcms.com/.$matches[2].$this->CI->config->item("base_url")."cdms/".$this->moudle."/txtfile/$txtfile_id?pid=".$this->projectid.$matches[2].','.$other.$matches[4];
  189. }
  190. } else {
  191. return "window.open".$matches[1].$matches[2].$matches[3].$matches[4];
  192. }
  193. }
  194. private function css_replace( $matches ) {
  195. if(count($matches) < 5) return '';
  196. if( empty($matches[4]) ) return '';
  197. $matches[4] = rtrim($matches[4],'\'"/');
  198. //获取图片的id
  199. $parent_dir_num = substr_count( $matches[4], '../');
  200. $relative_dirname = $this->relative_dirname;
  201. for($i=0; $i<$parent_dir_num; $i++) {
  202. $relative_dirname = substr( $relative_dirname, 0, strrpos($relative_dirname,"/") );
  203. }
  204. $relativepath = rtrim($relative_dirname,'/') . '/'.ltrim($matches[4],'./');
  205. $image_id = $this->CI->model->get_id_by_path_and_project($relativepath,$this->projectid);
  206. //输出
  207. if( !empty($image_id) ) {
  208. if($this->moudle == 'dresult') {
  209. 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];
  210. } else {
  211. 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];
  212. }
  213. } else {
  214. return "background".$matches[1]."url".$matches[2].$matches[3].$matches[4].$matches[3].$matches[5];
  215. }
  216. }
  217. }
  218. /* End of Myreplace.php */
  219. /* Location: /application/libraries/Myreplace.php */

人气教程排行