php手记

function title()

{

.....代码

return $head

}

return 后加需要返回的值

  • php判断url是否可以打开

<?php
$url = "/upload/201110/20111008192257383.gif";
$array = get_headers($url,1);
if(preg_match('/200/',$array[0])){
echo "<pre/>";
print_r($array);
}else{
echo "无效url资源!";
}
?>

  • php抓取指定网页全部代码

<?php

$url='http://www.baidu.com/';

echo file_get_contents( $url );

?>

  • PHP抓取修改替换

<?
$url = '目标网址';
$lines_array = file($url);
$lines_string = implode('',
$lines_array); eregi("抓取开始代码(.*)抓取结束代码", $lines_string, $head);
$head = str_replace(array("删除字符1","删除字符2","删除字符3","删除字符4"),"",$head);
$str = str_replace("查找代码1","替换代码1",$head[0]);
$str = str_replace("查找代码2","替换代码2",$str);
$str = str_replace("查找代码3","替换代码3",$str);
echo $str;
?>

  • PHP正则表达式抓取网页


<?
$url = '目标网址';
$lines_array = file($url);
$lines_string = implode('',
$lines_array); eregi("抓取开始代码(.*)抓取结束代码", $lines_string, $head);
$head = str_replace(array("删除字符1","删除字符2"),"",$head);

$str3 = preg_replace("(正则表达式查找)","正则表达式替换",$head[0]);
echo $str3;
?>

  • php抓取页面并转编码

<?php
$url = 'http://www.kuang168.com/quote/';
$lines_array = file($url);
$lines_string = implode('',
$lines_array); eregi("<div class=\"subline\">
   <ul>(.*)</ul>
   </div>
  </td>", $lines_string, $head);
$head = str_replace(array("<div class=\"subline\">
   <ul>","</ul>
   </div>
  </td>","<span class=\"f_r\">","删除字符4"),"",$head);
$str = str_replace("</span>","&nbsp;",$head[0]);
$str = str_replace("查找代码2","替换代码2",$str);
$str = str_replace("查找代码3","替换代码3",$str);
$str = mb_convert_encoding($str,'utf8','gb2312');
echo $str;
?>

{dede:php}
$thisid = $refObj->Fields['id'];
print_r($thisid);
{/dede:php}

  • php判断是否包含


<?php
$url = "http://www.hack001.com/news/article_edit.php";
if(strpos($url,"article_edit") > 0)
{
    echo "包含";
}
?>

  • php判断长度

<?php
$url='http://www.hack001.com/news/plus/view.php?aid=';
echo strlen($url);
?>

  • php连接mssql,查询sql语句然后循环输出

<?php
 $conn=mssql_connect("127.0.0.1","root","123456");

 

mssql_select_db("sqldb"); 

$sql="select top 10 h.BuildID id,h.BuildName loupan,a.AreaName quyu,convert(int,h.BuildPrice) jiage,h.BuildKaipanDate kaipanshijian from dbo.Ant_Area a,dbo.Ant_HouseBuild h where h.BuildAreaID=a.AreaID order by h.BuildID desc";

$query = mssql_query($sql,$conn);

 

while($row=mssql_fetch_array($query)){
?>
<?=$row[loupan]?>
<?=$row[quyu]?>
<?=$row[jiage]?>
<?=$row[kaipanshijian]?>
<br>
<?
}
?>


<?
mssql_close($conn);
?>

 


  • php显示错误

有时候php只显示500错误,而不说哪个地方错了,需要看错误信息可以这样操作

调试的时候把php.ini中的display_errors = Off改成On 
或者 error_log = D:\Web\error.log 在error.log中查看错误日志!