php正则替换处理HTML页面的方法,php替换html页面
本文实例讲述了php正则替换处理HTML页面的方法。分享给大家供大家参考。具体如下:
<?php
if(!defined('BASEPATH')) exit('No direct script access allowed');
/**
* HTML替换处理类,考虑如下几种替换
* 1. img src : '/]+?)/i'
* 2. a href : '/]+?)/i'
* 3. ifram.src : '/
输出
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);
//输出
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],'#'));
//获取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 "