类似html解析,利用php也做出比较智能的模式.
<?php
header('Content-type:text/html; charset=utf-8');
class html {
protected $html_text;
public function html($str=null) {
if($str===null)
return false;
$this->html_text = $str;
}
public function get($key='div')
{
$keyrep = $this->rep($key);
preg_match_all($keyrep,$this->html_text,$arr);
foreach ($arr[0] as $skey => $val)
$arr[0][$skey] = $this->htmlnum($val,$key);
return $arr[0];
}
protected function rep($key){
return sprintf('/<%s.*<\/%s>/isU',$key,$key);
}
protected function htmlnum($val,$key)
{
$key1 = substr_count($val,'<'.$key);
$key2 = substr_count($val,'</'.$key.'>');
if($key1!==$key2){
$val .= str_repeat('</'.$key.'>',$key1-$key2);
}
return $val;
}
public function curl($Url){
set_time_limit(0);
magic_quotes_runtime(0);
return file_get_contents($Url);
}
}
//实例。
$html = new html();
//获取文件。
$file = $html->curl('http://bbs.phpchina.com/index.php'); //获取文件;
//把内容放进类中。
$html->html($file);
//取得html元素的所有值。
$arr = $html->get('a');
print_r($arr);
?>
<?php
header('Content-type:text/html; charset=utf-8');
class html {
protected $html_text;
public function html($str=null) {
if($str===null)
return false;
$this->html_text = $str;
}
public function get($key='div')
{
$keyrep = $this->rep($key);
preg_match_all($keyrep,$this->html_text,$arr);
foreach ($arr[0] as $skey => $val)
$arr[0][$skey] = $this->htmlnum($val,$key);
return $arr[0];
}
protected function rep($key){
return sprintf('/<%s.*<\/%s>/isU',$key,$key);
}
protected function htmlnum($val,$key)
{
$key1 = substr_count($val,'<'.$key);
$key2 = substr_count($val,'</'.$key.'>');
if($key1!==$key2){
$val .= str_repeat('</'.$key.'>',$key1-$key2);
}
return $val;
}
public function curl($Url){
set_time_limit(0);
magic_quotes_runtime(0);
return file_get_contents($Url);
}
}
//实例。
$html = new html();
//获取文件。
$file = $html->curl('http://bbs.phpchina.com/index.php'); //获取文件;
//把内容放进类中。
$html->html($file);
//取得html元素的所有值。
$arr = $html->get('a');
print_r($arr);
?>
作者:noel@淘宝网女装新款秋装连衣裙裤子外套上衣_2012时尚女装新款 Ecmall二次开发-PHP技术
地址:http://www.laohucheng.com/post/150/
版权所有©转载时必须以链接形式注明作者和原始出处及本声明!
Tags: php类似html解析 引用(0)
SQL 注入
php mssql 数据库分页SQL语句
2009
11:43
415
0


