PHP to replace the way to process HTML pages

Source: Internet
Author: User
Tags config php programming readfile rtrim

The example in this article describes how PHP is replacing the processing of HTML pages. Share to everyone for your reference. Specifically as follows:

?

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

I hope this article will help you with your PHP programming.

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.