This article mainly introduces the php regular expression replacement method for processing HTML pages, involving php's matching techniques for common html page elements, for more information about how to use php regular expression replacement to process HTML pages, see the following example. Share it with you for your reference. The details are as follows:
<? Phpif (! Defined ('basepath') exit ('no direct script access allowed');/*** replace the processing class with HTML, consider replacing the following types * 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', 'tresresult', 'dresresult'); private $ content; private $ relative_dirname; private $ projectid; private $ moudle; function _ construct () {$ this-> CI = & get_instance ();} /*** replace * @ param string $ content HTML content * @ param string $ relative path * @ param int $ projectid Project id * @ moudle string $ 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'); br Eak; case 'result': $ this-> CI-> load-> model ('taskresult', 'model'); break; case 'dresresult ': $ 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], '\' "/'); // Obtain the image id $ 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 "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 "0) $ matches [3] = substr ($ matches [3], 0, strrpos ($ matches [3], '#'); // Obtain the html id $ 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_di Rname ,'/'). '/'. 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 = 'dresresult') {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"
0) $ matches [3] = substr ($ matches [3], 0, strrpos ($ matches [3], '#'); // Obtain the html id $ 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-> m Odel-> get_id_by_path_and_project ($ relativepath, $ this-> projectid); // output if (! Empty ($ txtfile_id) {if ($ this-> moudle = 'dresresult') {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"
0) return "window. open ". $ matches [1]. $ matches [2]. $ matches [3]. $ matches [4]; // Obtain the html id $ 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-> mode L-> 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], '\'"/'); // Obtain the image id $ parent_dir_num = substr_count ($ matches [4], '.. /'); $ relative_dirname = $ this-> relative_dir Name; 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 php programming.