`

PHP 解析HTML元素实例 -- vb2005xu PHP进阶教程之一

阅读更多
在网上一搜 解析HTML元素 十之八九都会转到与JAVA相关的页面,其实PHP才是解析这些内容最简单的方案,这里仅仅提供一个例子,其他的请举一反三!


//获取HTML文件中title的值
function getHtmlFileTitle($htmlFileName)
{
 $file=file($htmlFileName); 
 $count=count($file); 

 for($i=0;$i<$count;$i++){ 
  if(eregi("<title>(.*)</title>",$file[$i],$out)){ 
   $title=$out[0]; 
  } 
 } 
 $title=substr($title,7,-8); 
 return $title;
}




<html xmlns="http://www.w3.org/1999/xhtml"><head><meta http-equiv="Content-Type" content="text/html; charset=UTF-8" />
</head>
<body>
<?php
//获取HTML文件中title的值
function getHtmlFileTitle($htmlFileName)
{
 $file=file($htmlFileName); 
 $count=count($file); 

 for($i=0;$i<$count;$i++){ 
  if(eregi("<title>(.*)</title>",$file[$i],$out)){ 
   $title=$out[0]; 
  } 
 } 
 $title=substr($title,7,-8); 
 return $title;
}

if (! isset($_REQUEST['url']) || empty($_REQUEST['url']))
	die( "参数实例: url=www.ys168.com " ) ;

echo getHtmlFileTitle("http://{$_REQUEST['url']}") ;

?>

</body>
</html>

3
1
分享到:
评论
1 楼 vb2005xu 2008-10-09  
hite 写道

为什么要在变量名前面加$呢?麻烦不麻烦?php设计者什么考虑啊?


和PHP的起源有关系吧 毕竟他是从shell awk perl脚本改进过来的,就像JAVA为什么类似于C/C++的语法一样,可以降低程序员学习的成本和快速上手的原因吧 同样的道理PHP就比javascript python学起来要简单的多了

相关推荐

Global site tag (gtag.js) - Google Analytics