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

首頁 > 網站 > 建站經驗 > 正文

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

2024-04-25 20:37:29
字體:
來源:轉載
供稿:網友

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

<?php

if(!defined('BASEPATH')) exit('No direct script access allowed');

/**

* HTML替換處理類,考慮如下幾種替換

* 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 $relative_dirname;

private $projectid;

private $moudle;

function __construct() {

$this->CI = &get_instance ();

}

/**

* 替換

* @param string $content HTML內容

* @param string $relative 相對路徑

* @param int $projectid 項目id

* @moudle string $moudle 模板標識: 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 = '/<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 '';

$matches[3] = rtrim($matches[3],'/'"/');

//獲取圖片的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);

//輸出

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];

}

}

private function html_replace( $matches ) {

if(count($matches) < 4) return '';

if( empty($matches[3]) ) return '';

//如果href的鏈接($matches[3])以http或www或mailto開始,則不進行處理

//if(preg_match('/^[http|www|mailto](.+?)/i',$matches[3]))

// return "<a".$matches[1]."href=".$matches[2].$matches[3].$matches[4];

$matches[3] = rtrim($matches[3],'/'"/');

//處理錨點

if(substr_count($matches[3],'#')>0)

$matches[3] = substr($matches[3],0,strrpos($matches[3],'#'));

//獲取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->model->get_id_by_path_and_project($relativepath,$this->projectid);

//輸出

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],'/'"/');

//處理錨點

if(substr_count($matches[3],'#')>0)

$matches[3] = substr($matches[3],0,strrpos($matches[3],'#'));

//獲取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->model->get_id_by_path_and_project($relativepath,$this->projectid);

//輸出

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];

}

}

private function frame_replace( $matches ) {

if(count($matches) < 4) return '';

if( empty($matches[3]) ) return '';

$matches[3] = rtrim($matches[3],'/'"/');

//處理錨點

if(substr_count($matches[3],'#')>0)

$matches[3] = substr($matches[3],0,strrpos($matches[3],'#'));

//獲取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->model->get_id_by_path_and_project($relativepath,$this->projectid);

//輸出

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];

}

}

private function js_replace( $matches ){

if(count($matches) < 4) return '';

if( empty($matches[3]) ) return '';

//處理鏈接

$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,'/'/"');

//處理錨點

if(substr_count($href,'#')>0)

return "window.open".$matches[1].$matches[2].$matches[3].$matches[4];;

//獲取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->model->get_id_by_path_and_project($relativepath,$this->projectid);

//輸出

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],'/'"/');

//獲取圖片的id

$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);

//輸出

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 */

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

發表評論 共有條評論
用戶名: 密碼:
驗證碼: 匿名發表
亚洲香蕉成人av网站在线观看_欧美精品成人91久久久久久久_久久久久久久久久久亚洲_热久久视久久精品18亚洲精品_国产精自产拍久久久久久_亚洲色图国产精品_91精品国产网站_中文字幕欧美日韩精品_国产精品久久久久久亚洲调教_国产精品久久一区_性夜试看影院91社区_97在线观看视频国产_68精品久久久久久欧美_欧美精品在线观看_国产精品一区二区久久精品_欧美老女人bb
欧美国产日韩中文字幕在线| 亚洲欧洲av一区二区| 亚洲国产精品va| 日本午夜精品理论片a级appf发布| 成人午夜高潮视频| 日韩久久免费电影| 911国产网站尤物在线观看| 亚洲影视中文字幕| 韩国19禁主播vip福利视频| 日本精品一区二区三区在线| 日韩成人在线播放| 久久久亚洲影院你懂的| 国产精品香蕉国产| 久久久亚洲网站| 97**国产露脸精品国产| 成人精品视频在线| 欧美另类69精品久久久久9999| 日韩精品视频在线观看网址| 欧美电影院免费观看| 亚洲国产私拍精品国模在线观看| 亚洲一区二区免费在线| 6080yy精品一区二区三区| 欧美日韩中文在线观看| 久久久亚洲精选| 国产有码在线一区二区视频| 日韩视频免费看| 久久久久久久久久久免费精品| 久久精品国产亚洲精品| 国外日韩电影在线观看| 午夜精品视频在线| 欧美一级免费视频| 日韩欧美在线观看视频| 色综合视频一区中文字幕| 日韩大片免费观看视频播放| 国产福利视频一区| 亚洲va电影大全| 国产精品无码专区在线观看| 亚洲精品久久久久中文字幕欢迎你| 成人妇女免费播放久久久| 一区二区三区四区视频| 亚洲天堂成人在线视频| 欧美亚洲在线播放| 欧美激情视频一区| 日韩av在线精品| 欧美在线视频一区二区| 最好看的2019的中文字幕视频| 色无极亚洲影院| 韩国国内大量揄拍精品视频| 亚洲欧美一区二区三区四区| 亚洲黄色免费三级| 日韩欧美国产中文字幕| 成人黄色av网站| 欧美日韩免费在线| 久久久久久久久久久免费| www欧美日韩| 韩国视频理论视频久久| 97超视频免费观看| 久久精品国产一区二区三区| 5278欧美一区二区三区| 亚洲奶大毛多的老太婆| 91av视频在线观看| 国产亚洲精品久久久久久牛牛| 久久这里只有精品99| 亚洲国产天堂久久国产91| 亚洲成人三级在线| 色妞久久福利网| 中文国产成人精品| 亚洲欧美国产高清va在线播| 国产精品嫩草影院久久久| 精品伊人久久97| 亚洲影院污污.| 欧美成人网在线| 亚洲电影成人av99爱色| 成人夜晚看av| 欧美大片免费观看在线观看网站推荐| 国产成人在线亚洲欧美| 超碰精品一区二区三区乱码| 亚洲尤物视频网| 91精品在线播放| 国产成人精品综合| 欧美性在线观看| 亚洲电影免费观看高清完整版在线观看| 欧美精品免费在线观看| 成人免费网站在线观看| 少妇高潮久久77777| 久久精品人人做人人爽| 午夜精品在线观看| 久久久精品国产网站| 日韩av在线影视| 亚洲第一区中文字幕| 91久久精品在线| 成人福利网站在线观看11| 黄色一区二区三区| 亚洲激情视频在线| 日韩最新免费不卡| 亚洲综合国产精品| 91在线免费观看网站| 国产精品精品国产| 国产精品福利久久久| 欧洲亚洲女同hd| 91在线观看免费高清完整版在线观看| 久久精品成人欧美大片| 午夜精品久久久久久久男人的天堂| 亚洲国产91色在线| 亚洲精品久久视频| 欧美亚洲在线视频| 亚洲性视频网站| 日本国产高清不卡| 国产亚洲视频在线| 精品亚洲一区二区三区在线观看| 91精品国产高清久久久久久久久| 成人性生交大片免费观看嘿嘿视频| 亚洲国产欧美一区二区三区久久| 中文字幕亚洲无线码a| 97国产精品视频人人做人人爱| 久久97精品久久久久久久不卡| 国产性色av一区二区| 欧美成人性色生活仑片| 激情av一区二区| 欧美另类99xxxxx| 国产盗摄xxxx视频xxx69| 国产精品久久久一区| 国产成人jvid在线播放| 亚洲精品免费在线视频| 国产精品夜间视频香蕉| 久久久精品视频在线观看| 日韩在线高清视频| 国产一区二区三区四区福利| 国产v综合ⅴ日韩v欧美大片| 影音先锋日韩有码| 欧美日韩美女视频| 亚洲第一天堂av| 亚洲国产精品va在线看黑人| 麻豆国产精品va在线观看不卡| 中文字幕少妇一区二区三区| 成人综合国产精品| 亚洲激情自拍图| 91久久在线视频| 欧美精品一本久久男人的天堂| 国语自产精品视频在线看抢先版图片| 亚洲肉体裸体xxxx137| 91豆花精品一区| 91在线观看欧美日韩| 国产精品久久久久不卡| 一区二区欧美在线| 永久免费毛片在线播放不卡| 国产精品久久久久不卡| 国产精品1区2区在线观看| 欧美精品少妇videofree| 九九热99久久久国产盗摄| 青青a在线精品免费观看| 亚洲国产精品嫩草影院久久| 日韩av网站导航| 夜夜躁日日躁狠狠久久88av| 亚洲天堂日韩电影| 日韩网站免费观看| 欧洲午夜精品久久久| 亚洲成人在线视频播放| 日韩欧美一区二区在线| 国产精品福利小视频| 色噜噜狠狠狠综合曰曰曰| 亚洲色图综合网| 国产精品88a∨| 最近2019中文免费高清视频观看www99|