亚洲香蕉成人av网站在线观看_欧美精品成人91久久久久久久_久久久久久久久久久亚洲_热久久视久久精品18亚洲精品_国产精自产拍久久久久久_亚洲色图国产精品_91精品国产网站_中文字幕欧美日韩精品_国产精品久久久久久亚洲调教_国产精品久久一区_性夜试看影院91社区_97在线观看视频国产_68精品久久久久久欧美_欧美精品在线观看_国产精品一区二区久久精品_欧美老女人bb

首頁 > 開發 > PHP > 正文

php正則替換處理HTML頁面的方法

2024-05-04 23:36:21
字體:
來源:轉載
供稿:網友

這篇文章主要介紹了php正則替換處理HTML頁面的方法,涉及php針對html頁面常見元素的匹配技巧,需要的朋友可以參考下

本文實例講述了php正則替換處理HTML頁面的方法。分享給大家供大家參考。具體如下:

 

 
  1. <?php 
  2. if(!defined('BASEPATH')) exit('No direct script access allowed'); 
  3. /** 
  4. * HTML替換處理類,考慮如下幾種替換 
  5. * 1. img src : '/<img(.+?)src=([/'/" ])?(.+?)([ >]+?)/i' 
  6. * 2. a href : '/<a(.+?)href=([/'/" ])?(.+?)([ >]+?)/i' 
  7. * 3. ifram.src : '/<iframe(.+?)src=([/'/" ])?(.+?)([ >]+?)/i' 
  8. * 4. frame src : '/<frame(.+?)src=([/'/" ])?(.+?)([ >]+?)/i' 
  9. * 5. js : '/window.open([( ]+?)([/'" ]+?)(.+?)([ )+?])/i' 
  10. * 6. css : '/background(.+?)url([( ])([/'" ]+?)(.+?)([ )+?])/i' 
  11. */ 
  12. class Myreplace { 
  13. private $moudle_array = array('udata','tdata','tresult','dresult'); 
  14. private $content
  15. private $relative_dirname
  16. private $projectid
  17. private $moudle
  18. function __construct() { 
  19. $this->CI = &get_instance (); 
  20. /** 
  21. * 替換 
  22. * @param string $content HTML內容 
  23. * @param string $relative 相對路徑 
  24. * @param int $projectid 項目id 
  25. * @moudle string $moudle 模板標識: udata,tdata,tresult,dresult 
  26. */ 
  27. public function my_replace($content,$relative,$projectid,$moudle) { 
  28. $this->content = $content
  29. $this->relative_dirname = $relative
  30. $this->projectid = $projectid
  31. if(in_array(strtolower($moudle),$this->moudle_array)) 
  32. $this->moudle = $moudle
  33. else exit
  34. switch($this->moudle) { 
  35. case 'udata'
  36. $this->CI->load->model('mupload_data','model'); 
  37. break
  38. case 'tdata'
  39. $this->CI->load->model('taskdata','model'); 
  40. break
  41. case 'tresult'
  42. $this->CI->load->model('taskresult','model'); 
  43. break
  44. case 'dresult'
  45. $this->CI->load->model('dmsresult','model'); 
  46. break
  47. default
  48. break
  49. $pattern = '/<img(.+?)src=([/'/" ])?(.+?)([ >]+?)/i'
  50. $content = preg_replace_callback( $patternarray($this'image_replace') , $content ); 
  51. $pattern = '/<a(.+?)href=([/'/" ])?(.+?)([ >]+?)/i'
  52. $content = preg_replace_callback( $patternarray($this'html_replace') , $content ); 
  53. $pattern = '/<iframe(.+?)src=([/'/" ])?(.+?)([ >]+?)/i'
  54. $content = preg_replace_callback( $patternarray($this'iframe_replace') , $content ); 
  55. $pattern = '/<frame(.+?)src=([/'/" ])?(.+?)([ >]+?)/i';  
  56. $content = preg_replace_callback( $patternarray($this'frame_replace'), $content ); 
  57. $pattern = '/window.open([( ]+?)([/'" ]+?)(.+?)([ )]+?)/i'
  58. $content = preg_replace_callback( $patternarray($this'js_replace'), $content ); 
  59. $pattern = '/background(.+?)url([( ])([/'" ]+?)(.+?)([ )+?])/i'
  60. $content = preg_replace_callback( $patternarray($this'css_replace'), $content); 
  61. return $content
  62. private function image_replace($matches) { 
  63. if(count($matches) < 4) return ''
  64. ifemptyempty($matches[3]) ) return ''
  65. $matches[3] = rtrim($matches[3],'/'"/'); 
  66. //獲取圖片的id 
  67. $parent_dir_num = substr_count( $matches[3], '../'); 
  68. $relative_dirname = $this->relative_dirname; 
  69. for($i=0; $i<$parent_dir_num$i++) { 
  70. $relative_dirname = substr$relative_dirname, 0, strrpos($relative_dirname,"/") ); 
  71. $relativepath = rtrim($relative_dirname,'/') . '/'.ltrim($matches[3],'./'); 
  72. $image_id = $this->CI->model->get_id_by_path_and_project($relativepath,$this->projectid); 
  73. //輸出 
  74. if( !emptyempty($image_id) ) { 
  75. if($this->moudle == 'dresult') { 
  76. return "<img".$matches[1]."src=".$matches[2].$this->CI->config->item("base_url")."cdms/".$this->moudle."/readpic/$image_id?pid=".$this->projectid .$matches[2]. $matches[4]; 
  77. else { 
  78. return "<img".$matches[1]."src=".$matches[2].$this->CI->config->item("base_url")."cdms/".$this->moudle."/picfile/$image_id?pid=".$this->projectid .$matches[2]. $matches[4]; 
  79. else { 
  80. return "<img".$matches[1]."src=".$matches[2].$matches[3].$matches[2].$matches[4]; 
  81. private function html_replace( $matches ) { 
  82. if(count($matches) < 4) return ''
  83. ifemptyempty($matches[3]) ) return ''
  84. //如果href的鏈接($matches[3])以http或www或mailto開始,則不進行處理 
  85. //if(preg_match('/^[http|www|mailto](.+?)/i',$matches[3]))  
  86. // return "<a".$matches[1]."href=".$matches[2].$matches[3].$matches[4]; 
  87. $matches[3] = rtrim($matches[3],'/'"/'); 
  88. //處理錨點 
  89. if(substr_count($matches[3],'#')>0)  
  90. $matches[3] = substr($matches[3],0,strrpos($matches[3],'#')); 
  91. //獲取html的id 
  92. $parent_dir_num = substr_count( $matches[3], '../'); 
  93. $relative_dirname = $this->relative_dirname; 
  94. for($i=0; $i<$parent_dir_num$i++) { 
  95. $relative_dirname = substr$relative_dirname, 0, strrpos($relative_dirname,"/") ); 
  96. $relativepath = rtrim($relative_dirname,'/') . '/'.ltrim($matches[3],'./'); 
  97. $txtfile_id = $this->CI->model->get_id_by_path_and_project($relativepath,$this->projectid); 
  98. //輸出 
  99. if( !emptyempty($txtfile_id ) ) { 
  100. if($this->moudle == 'dresult') { 
  101. return "<a".$matches[1]."href=".$matches[2].$this->CI->config->item("base_url")."cdms/".$this->moudle."/readfile/$txtfile_id?pid=".$this->projectid .$matches[2].$matches[4]; 
  102. else { 
  103. return "<a".$matches[1]."href=".$matches[2].$this->CI->config->item("base_url")."cdms/".$this->moudle."/txtfile/$txtfile_id?pid=".$this->projectid .$matches[2].$matches[4]; 
  104. else { 
  105. return "<a".$matches[1]."href=".$matches[2].$matches[3].$matches[2].$matches[4]; 
  106. private function iframe_replace( $matches ) { 
  107. if(count($matches) < 4) return ''
  108. ifemptyempty($matches[3]) ) return ''
  109. $matches[3] = rtrim($matches[3],'/'"/'); 
  110. //處理錨點 
  111. if(substr_count($matches[3],'#')>0)  
  112. $matches[3] = substr($matches[3],0,strrpos($matches[3],'#')); 
  113. //獲取html的id 
  114. $parent_dir_num = substr_count( $matches[3], '../'); 
  115. $relative_dirname = $this->relative_dirname; 
  116. for($i=0; $i<$parent_dir_num$i++) { 
  117. $relative_dirname = substr$relative_dirname, 0, strrpos($relative_dirname,"/") ); 
  118. $relativepath = rtrim($relative_dirname,'/') . '/'.ltrim($matches[3],'./'); 
  119. $txtfile_id = $this->CI->model->get_id_by_path_and_project($relativepath,$this->projectid); 
  120. //輸出 
  121. if( !emptyempty($txtfile_id ) ) { 
  122. if($this->moudle == 'dresult') {  
  123. return "<iframe".$matches[1]."src=".$matches[2].$this->CI->config->item("base_url")."cdms/".$this->moudle."/readfile/$txtfile_id?pid=".$this->projectid .$matches[2].$matches[4]; 
  124. else { 
  125. return "<iframe".$matches[1]."src=".$matches[2].$this->CI->config->item("base_url")."cdms/".$this->moudle."/txtfile/$txtfile_id?pid=".$this->projectid .$matches[2].$matches[4]; 
  126. else { 
  127. return "<iframe".$matches[1]."src=".$matches[2].$matches[3].$matches[2].$matches[4]; 
  128. private function frame_replace( $matches ) {  
  129. if(count($matches) < 4) return ''
  130. ifemptyempty($matches[3]) ) return ''
  131. $matches[3] = rtrim($matches[3],'/'"/'); 
  132. //處理錨點 
  133. if(substr_count($matches[3],'#')>0)  
  134. $matches[3] = substr($matches[3],0,strrpos($matches[3],'#')); 
  135. //獲取html的id 
  136. $parent_dir_num = substr_count( $matches[3], '../'); 
  137. $relative_dirname = $this->relative_dirname; 
  138. for($i=0; $i<$parent_dir_num$i++) { 
  139. $relative_dirname = substr$relative_dirname, 0, strrpos($relative_dirname,"/") ); 
  140. $relativepath = rtrim($relative_dirname,'/') . '/'.ltrim($matches[3],'./'); 
  141. $txtfile_id = $this->CI->model->get_id_by_path_and_project($relativepath,$this->projectid); 
  142. //輸出 
  143. if( !emptyempty($txtfile_id ) ) { 
  144. if($this->moudle == 'dresult') {  
  145. return "<frame".$matches[1]."src=".$matches[2].$this->CI->config->item("base_url")."cdms/".$this->moudle."/readfile/$txtfile_id?pid=".$this->projectid.$matches[2].$matches[4]; 
  146. else { 
  147. return "<frame".$matches[1]."src=".$matches[2].$this->CI->config->item("base_url")."cdms/".$this->moudle."/txtfile/$txtfile_id?pid=".$this->projectid.$matches[2].$matches[4]; 
  148. else { 
  149. return "<frame".$matches[1]."src=".$matches[2].$matches[3].$matches[2].$matches[4]; 
  150. private function js_replace( $matches ){ 
  151. if(count($matches) < 4) return ''
  152. ifemptyempty($matches[3]) ) return ''
  153. //處理鏈接 
  154. $arr_html = split(',',$matches[3]); 
  155. $href = $arr_html[0]; 
  156. $other = ''
  157. for($i=0; $i<count($arr_html); $i++) 
  158. $other = $arr_html[$i].", "
  159. $other = rtrim($other,"/, "); 
  160. $href =rtrim($href,'/'/"'); 
  161. //處理錨點 
  162. if(substr_count($href,'#')>0)  
  163. return "window.open".$matches[1].$matches[2].$matches[3].$matches[4];; 
  164. //獲取html的id 
  165. $parent_dir_num = substr_count( $href'../'); 
  166. $relative_dirname = $this->relative_dirname; 
  167. for($i=0; $i<$parent_dir_num$i++) { 
  168. $relative_dirname = substr$relative_dirname, 0, strrpos($relative_dirname,"/") ); 
  169. $relativepath = rtrim($relative_dirname,'/') . '/'.ltrim($href,'./'); 
  170. $txtfile_id = $this->CI->model->get_id_by_path_and_project($relativepath,$this->projectid); 
  171. //輸出 
  172. if( !emptyempty($txtfile_id ) ) { 
  173. if($this->moudle == 'dresult') {  
  174. 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]; 
  175. else { 
  176. 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]; 
  177. else { 
  178. return "window.open".$matches[1].$matches[2].$matches[3].$matches[4]; 
  179. private function css_replace( $matches ) { 
  180. if(count($matches) < 5) return ''
  181. ifemptyempty($matches[4]) ) return ''
  182.  
  183. $matches[4] = rtrim($matches[4],'/'"/'); 
  184. //獲取圖片的id 
  185. $parent_dir_num = substr_count( $matches[4], '../'); 
  186. $relative_dirname = $this->relative_dirname; 
  187. for($i=0; $i<$parent_dir_num$i++) { 
  188. $relative_dirname = substr$relative_dirname, 0, strrpos($relative_dirname,"/") ); 
  189. $relativepath = rtrim($relative_dirname,'/') . '/'.ltrim($matches[4],'./'); 
  190. $image_id = $this->CI->model->get_id_by_path_and_project($relativepath,$this->projectid); 
  191. //輸出 
  192. if( !emptyempty($image_id) ) { 
  193. if($this->moudle == 'dresult') { 
  194. 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]; 
  195. else { 
  196. 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]; 
  197. else { 
  198. return "background".$matches[1]."url".$matches[2].$matches[3].$matches[4].$matches[3].$matches[5]; 
  199. /* End of Myreplace.php */ 
  200. /* Location: /application/libraries/Myreplace.php */ 

希望本文所述對大家的php程序設計有所幫助。

發表評論 共有條評論
用戶名: 密碼:
驗證碼: 匿名發表
亚洲香蕉成人av网站在线观看_欧美精品成人91久久久久久久_久久久久久久久久久亚洲_热久久视久久精品18亚洲精品_国产精自产拍久久久久久_亚洲色图国产精品_91精品国产网站_中文字幕欧美日韩精品_国产精品久久久久久亚洲调教_国产精品久久一区_性夜试看影院91社区_97在线观看视频国产_68精品久久久久久欧美_欧美精品在线观看_国产精品一区二区久久精品_欧美老女人bb
欧美成人自拍视频| 在线观看不卡av| 欧美特黄级在线| 九九久久国产精品| 欧美成人午夜剧场免费观看| 精品视频9999| 在线观看日韩av| 久久久久一本一区二区青青蜜月| 成人免费观看网址| 日本老师69xxx| 97精品欧美一区二区三区| 97免费中文视频在线观看| 欧美一区二区三区四区在线| 2021久久精品国产99国产精品| 97视频在线观看视频免费视频| 国产精品欧美激情在线播放| 国内精品久久久久久影视8| 久久天天躁狠狠躁夜夜躁| 黑人精品xxx一区一二区| 韩剧1988免费观看全集| 欧美激情视频播放| 黄色成人av网| 日韩精品在线观看网站| 日韩av电影国产| 国产精品自拍视频| 久久人体大胆视频| 国产精品视频久| 欧美一级淫片播放口| 久久精品国产v日韩v亚洲| xxxx欧美18另类的高清| 久久精品中文字幕免费mv| 欧美一级电影在线| 欧美剧在线观看| 在线观看日韩av| 国产91精品青草社区| 日韩av大片免费看| 国产97在线播放| 亚洲精品aⅴ中文字幕乱码| 国产自摸综合网| 亚洲性日韩精品一区二区| 亚洲乱码国产乱码精品精天堂| 成人激情综合网| 欧美乱人伦中文字幕在线| 亚洲欧美日韩中文在线制服| 亚洲色图色老头| 精品日本美女福利在线观看| 国产精品久久久久久一区二区| 岛国av一区二区在线在线观看| 97在线观看视频国产| 成人有码在线播放| 91麻豆桃色免费看| 日本高清久久天堂| 欧美黑人视频一区| 8050国产精品久久久久久| 九九精品视频在线| 狠狠干狠狠久久| 国产精品专区h在线观看| 欧美精品xxx| 亚洲女人初尝黑人巨大| 中文字幕免费国产精品| 欧美亚洲日本黄色| 国产日韩欧美另类| 精品久久久久久久久国产字幕| 色婷婷久久一区二区| 韩剧1988在线观看免费完整版| 国内精品免费午夜毛片| 久久久精品视频成人| 精品久久久久久久中文字幕| 亚洲免费一在线| 日韩av成人在线观看| 亚洲偷熟乱区亚洲香蕉av| 亚洲精品欧美日韩专区| 国产一区二区三区欧美| 黑人巨大精品欧美一区二区一视频| 久久国产精品首页| 国产精品日日做人人爱| 黄色成人在线免费| 国产精品自产拍高潮在线观看| 中文字幕日韩在线观看| 91精品在线观看视频| 日韩激情av在线免费观看| 日韩精品极品在线观看播放免费视频| 97视频在线观看视频免费视频| 国产日韩欧美综合| 久久影视电视剧凤归四时歌| 91九色在线视频| 在线免费观看羞羞视频一区二区| 国产高清在线不卡| 热久久视久久精品18亚洲精品| 一本色道久久综合亚洲精品小说| 国产精品一区二区久久久| 国产精品美女久久久久久免费| 美乳少妇欧美精品| 欧亚精品中文字幕| 成人免费看黄网站| 97香蕉超级碰碰久久免费的优势| 日韩精品在线观看一区二区| 精品久久久在线观看| 欧美视频在线免费看| 成人黄色av播放免费| 91精品久久久久久久久久入口| 亚洲精品电影久久久| 亚洲福利在线视频| 亚洲第一区在线| 久久久久久中文字幕| 久久久久久中文| 欧美午夜精品在线| 欧美成人中文字幕在线| 狠狠久久五月精品中文字幕| 国产精品日本精品| 欧美日韩色婷婷| 国产精品美女久久久久av超清| 91在线播放国产| 6080yy精品一区二区三区| 久久久久久久电影一区| 日韩在线免费高清视频| 国产精品成人aaaaa网站| 日韩综合中文字幕| 国产91色在线|免| 亚洲欧美国产精品专区久久| 96sao精品视频在线观看| 日韩在线视频二区| 中文字幕亚洲欧美在线| 国产精品www| 久久99热这里只有精品国产| 国产精品v日韩精品| 成人字幕网zmw| 欧美精品videofree1080p| 亚洲欧美日韩国产成人| 精品国产区一区二区三区在线观看| 日韩中文字幕网| 精品动漫一区二区三区| 欧美乱妇40p| 91精品国产综合久久香蕉的用户体验| 国产精品爱啪在线线免费观看| 久久精视频免费在线久久完整在线看| 欧美大成色www永久网站婷| 成人网在线观看| 欧美日韩亚洲天堂| 欧美一级视频在线观看| 久久久伊人日本| 国产精品v片在线观看不卡| 国产精品免费视频久久久| 久久久噜噜噜久噜久久| 欧美激情精品久久久久久黑人| 国产精品久久久av| 亚洲在线www| 日韩经典中文字幕在线观看| 亚洲a一级视频| 亚洲福利视频二区| 欧美日韩精品在线视频| 色yeye香蕉凹凸一区二区av| 播播国产欧美激情| 91精品视频在线看| 久久精品国产v日韩v亚洲| 亚洲黄色av网站| 国产一区私人高清影院| 亚洲人永久免费| 日韩成人在线电影网| 夜夜嗨av色一区二区不卡| 国产成人aa精品一区在线播放| 欧美成人精品三级在线观看| 国产精品成人va在线观看| 中文日韩在线视频|