<?php
class PHPHtmlParser
{
public $content;
public $tag_before;
public $tag_end;
public $respone;
private $tag_before_pos;
private $tag_end_pos;
private $respone_len;
function PHPHtmlParser($content)
{
$this->content = $this->strReplace($content,$toback=0);
}
function Parser($tag_before,$tag_end)
{
$this->tag_before = $this->strReplace($tag_before);
$this->tag_end = $this->strReplace($tag_end);
$this->tag_before_pos = strpos($this->content,$this->tag_before) + strlen($this->tag_before);
$this->tag_end_pos = strpos($this->content,$this->tag_end,$this->tag_before_pos);
$this->respone_len = $this->tag_end_pos – $this->tag_before_pos;
$this->respone = substr($this->content,$this->tag_before_pos,$this->respone_len);
$this->respone = $this->strReplace($this->respone,1);
return $this->respone;
}
function strReplace($content,$toback=0)
{
if ($toback) {
//替换回车换行
$content = str_replace(“{/enter/}”,”\r\n”,$content);
//替换空白
$content = str_replace(“{/blank/}”,” “,$content);
}else {
//替换回车换行
$content = str_replace(“\r\n”,”{/enter/}”,$content);
//替换空白
$content = str_replace(” “,”{/blank/}”,$content);
//替换左“
// $content = str_replace(““”,”\”",$content);
// //替换右”
// $content = str_replace(“””,”\”",$content);
}
return $content;
}
}
$xml_content = file_get_contents(“http://localhost/simplehtmldom/eg.html”);
$parser = new PHPHtmlParser($xml_content);
$title = $parser->Parser(‘
<h3 id=”news_title”>’,'</h3>
;’);
$content = $parser->Parser(‘约’,'条’);
echo $content;
?>