Archive for the 'php' Category



php的timezone时区设定方法

废话不说,在需要的php程序里边加多一句:

date_default_timezone_set(‘PRC’);

测试的话可以这样写写:

<?php
date_default_timezone_set(‘PRC’);
?>

dreamhost.com那里是美国空间,跟 china 相差 8 小时,某个统计程序里以前手工设定,想寻找个一老永日的方法,估不到这样简单设定就能解决问题!

反过来一句: date_default_timezone_get()   可以获取现在服务器的时区.

Tag:

php的替换一次的str_replace函数

为了方便做seo用的, 把内页的关键字替换带链接的关键词指向首页,所以不能全替换,只替换一次:

php原来的str_replace函数是替换全部的,不能用,所以在网上找高人已写好的,本来想自己动手的,不过懒啊,(*^__^*) 嘻嘻……

php的替换一次的函数:

function str_replace_once($needle, $replace, $haystack) {

$pos = strpos($haystack, $needle);

if ($pos === false) {

return $haystack;

}

return substr_replace($haystack, $replace, $pos, strlen($needle));

}

最后的就是应用效果不错.

Tag:

Freebsd 7安装php需要注意的小问题

昨天又在虚拟机Vmware安装Freebsd 7了,为的是实验extmail邮件系统的安装包,

安装包在这里:http://www.extmail.org/forum/thread-8640-1-1.html  ,

顺利的安装完成,但在安装完毕后,在/usr/local/www/apache22/data 写了个info.php的文件,

Read the rest of this entry »

Tag:

今天把wordpress的post-template.php修改了

为的是把amazon的资料采集到wordpress时发生的摘要令到wordpress的模板错位, 自己记录下, 因为自己还不会编写wordpress的插件,所以先直接改源代码.

位置, wp-includes目录下,

修改内容在post-template.php的129行,改成:

Read the rest of this entry »

Tag:

用php从yahoo网页获取股票数据

过下准备写个股票机械交易系统,先搞掂数据采集这部分先,做了个用php从yahoo获取股票数据的例子, 运行在shell下,其实php做shell几好用的.

以下为源代码:

gupiao.php ,  获取600808马钢股份的数据.
——————————–

<?

include “Snoopy.class.inc”;

$url=”http://finance.cn.yahoo.com/q?s=600808.ss”;

$snoopy = new Snoopy;
$snoopy->agent = “Mozilla/4.0 (compatible; MSIE 6.0; Windows NT 5.1)”;
$snoopy->fetch($url);
$getstr=$snoopy->results;
$getstr=trim($getstr);

if($getstr==”")
{
print “No Data”;
print “\n”;
exit;
}

$str=mycut(“<span id=\”top_date\”>”,”<div class=\”ft\”>”,$getstr);
$pieces = explode(“</span>”, $str);
$ftime=$pieces[0];
$ftime=trim($ftime);
print “时间: “.$ftime;
print “\n”;

$stra=mycut(“最新价:”,”</”,$str);
$stra=$stra.”</”;
//print $stra;
$stra=mycut(“>”,”</”,$stra);
print “最新价: “.$stra;
print “\n”;

$strb=mycut(“今开盘:”,”</”,$str);
$strb=$strb.”</”;
$strb=mycut(“>”,”</”,$strb);
print “开盘价: “.$strb;
print “\n”;

$strc=mycut(“最高价:”,”</”,$str);
$strc=$strc.”</”;
$strc=mycut(“>”,”</”,$strc);
print “最高价: “.$strc;
print “\n”;

$strd=mycut(“最低价:”,”</li>”,$str);
$strd=mycut(“>”,”</”,$strd);
print “最低价: “.$strd;
print “\n”;

$stre=mycut(“前收盘:”,”</”,$str);
$stre=$stre.”</”;
$stre=mycut(“>”,”</”,$stre);
print “前收盘: “.$stre;
print “\n”;
function mycut($firststr,$laststr,$srcstr)
{
// $findstr=”";

$pos1 = strpos($srcstr, $firststr);

if ($pos1 === false)
{
return “”;
}

//print “pos1: “.$pos1.”\n”;
$pos2 = strpos($srcstr, $laststr, $pos1);

if ($pos2 === false)
{
return “”;
}

if ($pos2<$pos1)
{
return “”;
}

//print “pos2: “.$pos2.”\n”;
// $num=1;
$message=explode($firststr,$srcstr);
$message=explode($laststr,$message[1]);

return $message[0];
}

?>

还是用到php的Snoopy类, 在shell状态下输入php -q gupiao.php就可以执行,当然也可以运行在web下.

源代码下载,包含Snoopy类

Tag:

php的图片下载函数

做采集中,需要用到php下载图片,在网上收集了一下,做了两个, 其中第一个用到readfile, 这个函数需要你的虚拟主机php.ini的设定allow_url_fopen = On , 例如我现在用的Dreamhost主机就是allow_url_fopen = Off,而在网上有教怎样打开Dreamhost的allow_url_fopen =On, 所以建议使用第二个函数,不过需要到网上下载snoopy类,这个使用sock, 不怕有问题.

以下为代码:
——————
函数1

function getImage($url,$filename=”") {
if(!$url) return false;

if(!$filename) {
$ext=strrchr(strtolower($url),”.”) ;
if($ext!=”.gif” && $ext!=”.jpg” && $ext!=”.png”) return false ;
$str=explode(‘/’,$url) ;
$filename=$str[count($str)-1] ;
//print $filename.”\n”;
}

ob_start();
readfile($url);
$img = ob_get_contents();
ob_end_clean();
$fp2=@fopen($filename, “a”) ;
fclose($fp2) ;

return $filename;
}

用法:

getImage(“http://www.lpfrx.com/wp-content/themes/ProSense/images/rss-feed-icon.jpg”);

—————————————————

Read the rest of this entry »

Tag: