一聚教程网:一个值得你收藏的教程网站

最新下载

热门教程

php读取xml文件

时间:2008-03-28 编辑:简简单单 来源:一聚教程网

php读取xml文件
class HTTPRequest
{
    var 
$_fp;        
// HTTP socket
    
var $_url;        
// full URL
    
var $_host;        
// HTTP host
    
var $_protocol;    
// protocol (HTTP/HTTPS)
    
var $_uri;        
// request URI
    
var $_port;        
// port
    
    // scan url
    
function _scan_url
()
    {
        
$req $this->_url
;
        
        
$pos strpos($req'://'
);
        
$this->_protocol strtolower(substr($req0$pos
));
        
        
$req substr($req$pos+3
);
        
$pos strpos($req'/'
);
        if(
$pos === false
)
            
$pos strlen($req
);
        
$host substr($req0$pos
);
        
        if(
strpos($host':') !== false
)
        {
            list(
$this->_host$this->_port) = explode(':'$host
);
        }
        else 
        {
            
$this->_host $host
;
            
$this->_port = ($this->_protocol == 'https') ? 443 80
;
        }
        
        
$this->_uri substr($req$pos
);
        if(
$this->_uri == ''
)
            
$this->_uri '/'
;
    }
    
    
// constructor
    
function HTTPRequest($url
)
    {
        
$this->_url $url
;
        
$this->_scan_url
();
    }
    
    
// download URL to string
    
function DownloadToString
()
    {
        
$crlf "\r\n"
;
        
        
// generate request
        
$req 'GET ' $this->_uri ' HTTP/1.0' 
$crlf
            
.    'Host: ' $this->_host 
$crlf
            
.    $crlf
;
        
        
// fetch
        
$this->_fp fsockopen(($this->_protocol == 'https' 'ssl://' '') . $this->_host$this->_port
);
        
fwrite($this->_fp$req
);
        while(
is_resource($this->_fp) && $this->_fp && !feof($this->_fp
))
            
$response .= fread($this->_fp1024
);
        
fclose($this->_fp
);
        
        
// split header and body
        
$pos strpos($response$crlf $crlf
);
        if(
$pos === false
)
            return(
$response
);
        
$header substr($response0$pos
);
        
$body substr($response$pos strlen($crlf
));
        
        
// parse headers
        
$headers 
= array();
        
$lines explode($crlf$header
);
        foreach(
$lines as $line
)
            if((
$pos strpos($line':')) !== false
)
                
$headers[strtolower(trim(substr($line0$pos)))] = trim(substr($line$pos+1
));
        
        
// redirection?
        
if(isset($headers['location'
]))
        {
            
$http = new HTTPRequest($headers['location'
]);
            return(
$http->DownloadToString($http
));
        }
        else 
        {
            return(
$body
);
        }
    }
function 
dhtmlspecialchars($string
) {
  if(
is_array($string
)) {
   foreach(
$string as $key => $val
) {
    
$string[$key] = dhtmlspecialchars($val
);
   }
  } else {
     
$string preg_replace('/&((#(\d{3,5}|x[a-fA-F0-9]{4})|[a-zA-Z][a-z0-9]{2,5});)/''&\\1'
,
     
str_replace(array('&''"''<''>'), array('&''"''<''>'), $string
));
   }
      return 
$string
;
}
function 
returnSubstrings($text$openingMarker$closingMarker
) {
    
$openingMarkerLength strlen($openingMarker
);
    
$closingMarkerLength strlen($closingMarker
);
    
$result 
= array();
    
$position 0
;
    while ((
$position strpos($text$openingMarker$position)) !== false
) {
      
$position += $openingMarkerLength
;
      if ((
$closingMarkerPosition strpos($text$closingMarker$position)) !== false
) {
        
$result[] = substr($text$position$closingMarkerPosition $position
);
        
$position $closingMarkerPosition $closingMarkerLength
;
       }
    }
    return 
$result
;
}
function 
is_mail($Argv
){
        
$RegExp='/^[a-z0-9][a-z\.0-9-_]+@[a-z0-9_-]+(?:\.[a-z]{0,3}\.[a-z]{0,2}|\.[a-z]{0,3}|\.[a-z]{0,2})$/i'
;
        return 
preg_match($RegExp,$Argv)?$Argv:false
;

}
?>

热门栏目