html引入php文件中的函数,在b2core框架和simple_html_dom.php文件里面都有一个load()函数...

本文探讨了simple_html_dom.php文件中的load()函数如何使用正则表达式去除HTML标签内的内容,这对于网页内容抓取的影响。同时,对比了系统框架B2core中的load()函数,该函数用于动态加载类文件并实例化对象。这两个load()函数在功能上存在显著差异,前者处理HTML字符串,后者处理类加载。
摘要由CSDN通过智能技术生成

在simple_html_dom.php文件里面,有一个load()函数的定义。

// load html from string

function load($str, $lowercase=true, $stripRN=true, $defaultBRText=DEFAULT_BR_TEXT, $defaultSpanText=DEFAULT_SPAN_TEXT)

{

global $debugObject;

// prepare

$this->prepare($str, $lowercase, $stripRN, $defaultBRText, $defaultSpanText);

// strip out comments

$this->remove_noise("''is");

// strip out cdata

$this->remove_noise("''is", true);

// Per sourceforge http://sourceforge.net/tracker/?func=detail&aid=2949097&group_id=218559&atid=1044037

// Script tags removal now preceeds style tag removal.

// strip out

$this->remove_noise("'<\s*script[^>]*[^/]>(.*?)<\s*/\s*script\s*>'is");

$this->remove_noise("'<\s*script\s*>(.*?)<\s*/\s*script\s*>'is");

// strip out

$this->remove_noise("'<\s*style[^>]*[^/]>(.*?)<\s*/\s*style\s*>'is");

$this->remove_noise("'<\s*style\s*>(.*?)<\s*/\s*style\s*>'is");

// strip out preformatted tags

$this->remove_noise("'<\s*(?:code)[^>]*>(.*?)<\s*/\s*(?:code)\s*>'is");

// strip out server side scripts

$this->remove_noise("'(<\?)(.*?)(\?>)'s", true);

// strip smarty scripts

$this->remove_noise("'(\{\w)(.*?)(\})'s", true);

// parsing

while ($this->parse());

// end

$this->root->_[HDOM_INFO_END] = $this->cursor;

$this->parse_charset();

// make load function chainable

return $this;

}

注意到这里,上面的一个函数通过正则表达式去掉了标签内部的函数。如果,我们要抓取内部的内部的内容的时候,就不能再利用框架了。

对比,我们的系统框架b2core内部也有一个加载系统的文件或类的函数load()

/* B2 系统函数

* load($path,$instantiate) 可以动态载入对象,如:控制器、Model、库类等

* $path 是类文件相对 app 的地址

* $instantiate 为 False 时,仅引用文件,不实例化对象

* $instantiate 为数组时,数组内容会作为参数传递给对象

*/

function &load($path, $instantiate = TRUE )

{

$param = FALSE;

if(is_array($instantiate)) {

$param = $instantiate;

$instantiate = TRUE;

}

$file = explode('/',$path);

$class_name = array_pop($file);

$object_name = md5($path);

static $objects = array();

if (isset($objects[$object_name])) {

if($objects[$object_name] == TRUE && $instantiate == TRUE) {

if ($param == FALSE) return new $class_name();

return new $class_name($param);

}

return $objects[$object_name];

}

require(APP.$path.'.php');

if ($instantiate == FALSE) $objects[$object_name] = TRUE;

elseif ($param) $objects[$object_name] = new $class_name($param);

else $objects[$object_name] = new $class_name();

return $objects[$object_name];

}

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值