HTML Preview Regular Replace
- /**
- * HTML replacement processing class, consider the following several substitutions
- * 1. IMG src: '/]+?) /I '
- * 2. a href: '/]+? /I '
- * 3. IFRAM.SRC: '/ ]+?) /I '
- * 4. 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 $relative _dirname;
- Private $projectid;
- Private $moudle;
- function __construct () {
- $this->ci = &get_instance ();
- }
- /**
- * Replace
- * @param string $contentHTML content
- * @param string $relative relative path
- * @param int $projectid Project ID
- * @moudlestring $moudle template ID: Udata,tdata,tresult,dresult
- */
- Public Function My_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 ');
- Break
- Case ' Tdata ':
- $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 = '/]+? ') /I ';
- $content = Preg_replace_callback ($pattern, Array ($this, ' image_replace '), $content);
- $pattern = '/]+? ') /I ';
- $content = Preg_replace_callback ($pattern, Array ($this, ' html_replace '), $content);
- $pattern = '/ ]+?) /I ';
- $content = Preg_replace_callback ($pattern, Array ($this, ' iframe_replace '), $content);
- $pattern = '/ ]+?) /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 ';
- $matches [3] = RTrim ($matches [3], ' \ ' "/');
- Get the ID of a 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 "Ci->config->item (" Base_url ")." cdms/". $this->moudle." /readpic/$image _id?pid= ". $this->projectid. $matches [2]. $matches [4];
- } else {
- Return "Ci->config->item (" Base_url ")." cdms/". $this->moudle." /picfile/$image _id?pid= ". $this->projectid. $matches [2]. $matches [4];
- }
- } else {
- Return "}
- }
- Private Function Html_replace ($matches) {
- if (count ($matches) < 4) return ';
- if (Empty ($matches [3])) return ';
- If the href link ($matches [3]) starts with HTTP or www or mailto, it is not processed
- if (Preg_match ('/^[http|www|mailto] (. +?) /I ', $matches [3]))
- Return "$matches [3] = RTrim ($matches [3], ' \ '"/');
- Working with anchor points
- 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 "Ci->config->item (" Base_url ")." cdms/". $this->moudle." /readfile/$txtfile _id?pid= ". $this->projectid. $matches [2]. $matches [4];
- } else {
- Return "Ci->config->item (" Base_url ")." cdms/". $this->moudle." /txtfile/$txtfile _id?pid= ". $this->projectid. $matches [2]. $matches [4];
- }
- } else {
- Return "}
- }
- Private Function Iframe_replace ($matches) {
- if (count ($matches) < 4) return ';
- if (Empty ($matches [3])) return ';
- $matches [3] = RTrim ($matches [3], ' \ ' "/');
- Working with anchor points
- 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 " ci->config->item (" Base_url ")." cdms/". $this->moudle." /readfile/$txtfile _id?pid= ". $this->projectid. $matches [2]. $matches [4];
- } else {
- Return " ci->config->item (" Base_url ")." cdms/". $this->moudle." /txtfile/$txtfile _id?pid= ". $this->projectid. $matches [2]. $matches [4];
- }
- } else {
- Return "<>
- }
- }
- Private Function Frame_replace ($matches) {
- if (count ($matches) < 4) return ';
- if (Empty ($matches [3])) return ';
- $matches [3] = RTrim ($matches [3], ' \ ' "/');
- Working with anchor points
- 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 " ci->config->item (" Base_url ")." cdms/". $this->moudle." /readfile/$txtfile _id?pid= ". $this->projectid. $matches [2]. $matches [4];
- } else {
- Return " ci->config->item (" Base_url ")." cdms/". $this->moudle." /txtfile/$txtfile _id?pid= ". $this->projectid. $matches [2]. $matches [4];
- }
- } else {
- Return "<>
- }
- }
- Private Function Js_replace ($matches) {
- if (count ($matches) < 4) return ';
- if (Empty ($matches [3])) return ';
- Working with links
- $arr _html = Split (', ', $matches [3]);
- $href = $arr _html[0];
- $other = ";
- for ($i =0; $i
- $other = $arr _html[$i]. ",";
- $other = RTrim ($other, "\,");
- $href =rtrim ($href, ' \ ' \ ');
- Working with anchor points
- 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 ';
- $matches [4] = RTrim ($matches [4], ' \ ' "/');
- Get the ID of a 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 */
Copy Code |