首页 > 编程技术 > php

PHP 和 XML: 使用expat函数(二)

发布时间:2016-11-25 16:53

PHP 和 XML: 使用expat函数(二)
  让我们看一下实际处理这个文档的PHP代码。
/*NewsBoy : News system for the web written in PHP by Justin Grant (Web: jusgrant.cjb.net or justin.host.za.net Mail: justin@glendale.net)25 March V0.0.2 Converted Newsboy to a PHP class, allowing the layout to be easily modified. Also added made the HTML that is genrated a little easier to read.24 March V0.0.1 Just completed the intial version, very rough and basic.*/
class newsboy { var $xml_parser; var $xml_file; var $html; var $open_tag ; var $close_tag ;
//Class Constructor
function newsboy() { $this->xml_parser = ""; $this->xml_file = ""; $this->html = ""; $this->open_tag = array(
//these are the default settings but they are quite easy to modify
"NEWSBOY" => "nn", "STORY" => " ", "DATE" => "", "SLUG" => " ", "TEXT" => "", "PIC" => "", "NEWLINE" => "" ); $this->close_tag = array( "NEWSBOY" => "
nnn", "STORY" => "", "DATE" => "", "SLUG" => "
", "TEXT" => "n", "PIC" => " "
" ); }
//Class Destructor (has to be invoked manually as PHP does not support destructors)
function destroy() { xml_parser_free($this->xml_parser); }
//Class Members
function concat($str) { $this->html .= $str; }
function startElement($parser, $name, $attrs) { //global $open_tag; if ($format= $this->open_tag[$name]) { $this->html .= $format; } }
function endElement($parser, $name) { global $close_tag; if ($format= $this->close_tag[$name]) { $this->html .= $format; } }
function characterData($parser, $data) { $this->html .= $data; }
/* function PIHandler($parser, $target, $data) { //switch (strtolower($target)){ // case "php": eval($data); // break; //} }*/
bool in_array ( mixed needle, array haystack [, bool strict])
在 haystack 中搜索 needle,如果找到则返回 TRUE,否则返回 FALSE。
如果第三个参数 strict 的值为 TRUE 则 in_array() 函数还会检查 needle 的类型是否和 haystack 中的相同。
注: 如果 needle 是字符串,则比较是区分大小写的。
注: 在 PHP 版本 4.2.0 之前,needle 不允许是一个数组。
例子 1. in_array() 例子
$os = array ("Mac", "NT", "Irix", "Linux");
if (in_array ("Irix", $os)) {
print "Got Irix";
}
if (in_array ("mac", $os)) {
print "Got mac";
}

很有用的一些函数,你可以作为源码保存,然后以后避免重复编写。
文件读取函式
   //文件读取函式
   function PHP_Read($file_name) {
   $fd=fopen($file_name,r);
   while($bufline=fgets($fd, 4096)){
   $buf.=$bufline;
    }
   fclose($fd);
   return $buf;
    }
   ?>
文件写入函式
      //文件写入函式
   function PHP_Write($file_name,$data,$method="w") {
   $filenum=@fopen($file_name,$method);
   flock($filenum,LOCK_EX);
   $file_data=fwrite($filenum,$data);
   fclose($filenum);
   return $file_data;
    }
   ?>
静态页面生成函式
   //静态页面生成函式
   function phptohtm($filefrom,$fileto,$u2u=1){
   if($u2u==1){
   $data=PHP_Read($filefrom);
    }else{
   $data=$filefrom;
    }
   PHP_Write($fileto,$data);
   return true;
    }
   ?>
指定条件信息数量检索函式
      //指定条件信息数量检索函式
   function rec_exist($table,$where){
   $query="select count(*) as num from $table ".$where;
   $result=mysql_query($query) or die(nerror(1));
   $rowcount=mysql_fetch_array($result);
   $num=$rowcount["num"];
   if ($num==0){
   return false;
    }
   return $num;
    }
   ?>
目录删除函式
   //目录删除函式
   function del_DIR($directory){
   $mydir=dir($directory);
   while($file=$mydir->read()){
   if((is_dir("$directory/$file")) AND ($file!=".") AND ($file!="..")){
<?
echo "<p>__________________________<p>";
$string="242432反对感是456,,犯得上广泛大使馆地方7890abc";
$mb_strlen=mb_strlen($string);
$len=20;
echo $string."<p>";
echo "总长为:".($mb_strlen+1)."<p>";
echo "截取数:".$len."<p>";
for($i=1;$i<=$mb_strlen+1;$i++){
if($i>$len){
echo $i."<b> →</b> ".mb_substr($string,$i)."…<br>";
continue;
}
echo $i."<b> →</b> ".mb_substr($string,$i)."<br>";
}
?>
参看PHP的Multi-Byte String Functions,
(1)编译时使用--enable-mbstring=cn
(2)修改/usr/local/lib/php.inc
default_charset = "zh-cn"
mbstring.language = zh-cn
mbstring.internal_encoding =zh-cn
Everthing I do, I do it for you!
My linux!
原理:读取某站点的源文件,再利用正则分析其源代码,得到所有链接
<?
/**********qiushuiwuhen(2002-5-20)***********/
if(empty($url))$url = "http://www.111cn.net/expert/";//设置url
$site=substr($url,0,strpos($url,"/",8));//站点
$base=substr($url,0,strrpos($url,"/")+1);//文件所在目录
$fp = fopen($url, "r" );//打开url
while(!feof($fp))$contents.=fread($fp,1024);//
$pattern="|href=['"]?([^ '"]+)['" ]|U";
preg_match_all($pattern,$contents, $regArr, PREG_SET_ORDER);//匹配所有href=
for($i=0;$i<count($regArr);$i++){//遍历所有匹配
if(!eregi("://",$regArr[$i][1]))//是否是相对路径,即是否还有://
if(substr($regArr[$i][1],0,1)=="/")//是否是站点的根目录
echo "link".($i+1).":".$site.$regArr[$i][1]."<br/>";//根目录
else
echo "link".($i+1).":".$base.$regArr[$i][1]."<br/>";//当前目录
else
echo "link".($i+1).":".$regArr[$i][1]."<br/>";//相对路径
}
fclose($fp);
?>

标签:[!--infotagslink--]

您可能感兴趣的文章: