中山php|最优网络中山做网站 中山php建站

最优良人

2011/08/18 at 21:47

php函数体里调用外部变量

在程序开发中,有时在一个函数里面需要调用到函数体以外的变量,这个时候有几种方法

可以再声明变量的时候声明为全局变量,如:

global $string;

$string = 'test';

function __(){
return $string;
}

也可以在函数的内部声明,如:

$string = 'test';

function __(){

global $string;
return $string;
}

当需要调用的变量只有少数的时候可以这样用,那么如果是需要使用大量已经定义过的变量或者甚至是全部变量的时候如何处理呢?可以这样处理,用到PHP的超全局数组$GLOBALS和extract()函数

PHP手册对$GLOBAL的说明是这样的:

An associative array containing references to all variables which are currently defined in the global scope of the script. The variable names are the keys of the array.

Note: This is a 'superglobal', or automatic global, variable. This simply means that it is available in all scopes throughout a script. There is no need to do global $variable; to access it within functions or methods.

大概意思是:

这个一个由所有变量组成的数组。变量名就是该数组的索引。并且该数组是超全局数组,在使用时不必声明global $variable;

extract()函数的作用是把数组的键名作为变量名,数组的键值作为变量的值。

所以综上所述,只要在函数体里面写上下面一句话就可以实现调用到外部的所有变量了

$string = 'test';

$num = 100;

function __(){

echo$string,$num;
}

extract($GLOBALS,EXTR_SKIP);

标签:,
comments Comments (26)    -
2011/08/18 at 17:47

div li元素不限定宽度,如何浮在中间

div的横向居中我们都知道而已用margin:0 auto;实现,但是在不指定宽度的情况下这语句是没有意义的,div为块级元素,在内层DIV未指定宽度的情况下不能通过margin:0 auto来实现居中,将其转换为内联元素(display:inline)后在外层DIV加text-align:center才能居中
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=gb2312" />
<title>无标题文档</title>
</head>
<body>
<div style="width:800px;height:300px;text-align:center;margin:0 auto;background:#efefef;"><div style="background:#ff33cc;display:inline;">sdaasddsa</div></div>
</body>
</html>

标签:, , , ,
comments Comments (498)    -
2011/08/18 at 14:53

如何避免谷歌搜索被重置

使用google搜索时,经常遇到一件很纠结的事情就是突然搜索结果就被重置了,特别是使用谷歌搜索工具这个好用的东东的时候,基本上是用不了,想看最近24小时博客的收录情况,要换好几个浏览器,一般的解决方法是翻墙去搜索,但是速度不敢恭维。

最近发现一种比较简单的方法,就是通过遨游的多重搜索,在谷歌用搜索工具的时候暂时还没被墙过,暗喜……

comments Comments (190)    -
2011/08/18 at 14:42

按钮变成失效状态,input框变成只读模式以及CSS一些页面控制

以下代码可以使按钮变成失效状态
<input id="select-layout" type="button" value="布置图管理" name="select-layout" disabled="disabled">

以下代码可以使input框变成只读且禁用模式
<input name="demo" type="text" disabled value="value" readonly="true" />

CSS控制字数多,隐藏多余字
text-overflow:ellipsis;word-break:keep-all;overflow:hidden; white-space:nowrap;

显示竖的滚动条
overflow-y:auto;height:220px;

//表格不被撑开,换行
style="word-break:break-all"

控制input或者textarea背景透明的样式
background-color: transparent;

实现细边的表格
<table width=500 border="0" align="center" cellpadding="5" cellspacing="1" bgcolor="#add3ef">
<tr bgcolor="#eff3ff">
<td>标题:用户:</td>
</tr>
<tr bgColor="#ffffff">
<td>内容:</td>
</tr>
</table>
或者简单通过这个属性控制
style="border-collapse:collapse"

标签:
comments Comments (200)    -
2011/08/18 at 14:38

php获取远程图片,下载到本地-readfile,file_get_contents函数

php获取远程图片的原理是使用readfile函数读入一个远程文件的stream,然后写入一个文件生成本地图片

注:也可以用file_get_contents函数,二者的区别是readfile直接把文件stream输出,而后者赋给变量。

以下自定义函数可以实现远程图片获取,并自动下载为本地文件:

<?php
//
// Function: 获取远程图片并把它保存到本地
//
//
// 确定您有把文件写入本地服务器的权限
//
//
// 变量说明:
// $url 是远程图片的完整URL地址,不能为空。
// $filename 是可选变量: 如果为空,本地文件名将基于时间和日期
// 自动生成.
function GrabImage($url,$filename="") {
if($url==""):return false;endif;
if($filename=="") {
$ext=strrchr($url,".");
if($ext!=".gif" && $ext!=".jpg"):return false;endif;
$filename=date("dMYHis").$ext;
}
ob_start();
readfile($url);
$img = ob_get_contents();
ob_end_clean();
$size = strlen($img);
$fp2=@fopen($filename, "a");
fwrite($fp2,$img);
fclose($fp2);
return $filename;
}
$img=GrabImage("/Article/UploadFiles/201003/20100313135251414.jpg","");
if($img):echo '<pre><img src="'.$img.'"></pre>';
else:echo "false";
endif;
?>

标签:
comments Comments (16)    -
2011/08/18 at 14:26

把网站页面自动提交到搜索引擎

很多搜索引擎都提供了博客地址的提交入口,可以把我们博客的网址或者文章地址提交到他们的数据库以供索引,其中有些在提交过程中需要输入验证码,如百度,有道;有些则不需要,如谷歌,腾讯搜搜;不需要输入验证码的入口我们可以通过写程序把文章自动提交到搜索引擎。

下面是一些搜索引擎提供的博客搜索服务:
http://blogsearch.google.com/ 谷歌博客搜索

http://blogsearch.baidu.com/ 百度博客搜索

http://blog.soso.com/ 腾讯搜搜博客搜索:可以搜QQ空间
http://blog.youdao.com/ 网易有道博客搜索:有一些有趣的数据整理方式
下面则是这些搜索引擎相应提供的提交博客入口:
http://blogsearch.google.com/ping?hl=zh-CN google博客提交入口
http://utility.baidu.com/blogsearch/submit.php 百度博客提交入口,有验证码
http://tellbot.youdao.com/report?type=BLOG 有道博客提交入口,有验证码
http://blog.soso.com/join.html 腾讯搜搜博客搜索提交入口


博客ping服务地址:

ping是基于XML_RPC标准协议的更新通告服务,是用于blog在内容更新时通知博客搜索引擎及时进行抓取、更新的方式。博客搜索引擎在成功接受到ping以后,会立刻进行抓取并更新。使用ping服务,可以让博客搜索引擎在第一时间抓取到您博客上的新内容。

http://blogsearch.google.com/ping/rpc2 谷歌博客搜索ping服务
http://ping.baidu.com/cgi-bin/blog 百度博客搜索ping服务
http://blog.youdao.com/ping/RPC2 网易有道博客搜索ping服务

 

下面的代码是天涯博客实现博客的博文自动提交,其他网站页面的提交道理也是相同的,都是利用博客的自动ping服务:

 

<form id=frm_google_ping name=frm_google_ping action=http://blogsearch.google.com/ping method=get target="iframe__google_ping">
<input type="hidden" name="name" value="tianyablog" ID="Hidden4">
<input type="hidden" name="url" value="http://www.zui88.com/" ID="Hidden5">
<input type="hidden" name="changesURL" value="http://www.zui88.com/blog/about" ID="Hidden6">
</form>
<iframe name="iframe__google_ping" src="" width="0" height="0"></iframe>
<br><br>
<form id=frm_soso_ping name=frm_soso_ping action="http://tep.soso.com/cgi-bin/pingd.fcgi" method=get target="iframe_soso_ping">
<input type="hidden" name="soso_url" value="tianyablog" ID="sosoHidden4">
<input type="hidden" name="soso_url" value="http://www.zui88.com/ " ID="sosoHidden5">
<input type="hidden" name="soso_url" value="http://www.zui88.com/" ID="sosoHidden6">
</form>
<iframe name="iframe_soso_ping" src="" width="0" height="0"></iframe>

 

标签:
comments Comments (437)    -
2011/08/16 at 10:31

FleaPHP页面控件 WebControls的使用

如果不使用模板引擎,需要先注册控件 $ui =& FLEA::initWebControls() ;其实就是返回控件的实例,该函数的代码是

00662 function & initWebControls()
00663 {
00664 return FLEA::getSingleton(FLEA::getAppInf('webControlsClassName'));
00665 }

'webControlsClassName'默认是FLEA目录下的webControls类,该类封装了页面组件的实现,以及一些常用的页面控件,在找不到这些自带控件的时候就会去尝试搜索我们自定义的以_ctl开头的控件

/**
* 构造一个控件的 HTML 代码
*
* @param string $type
* @param string $name
* @param array $attribs
* @param boolean $return
*
* @return string
*/
function control($type, $name, $attribs = null, $return = false)
{
$type = strtolower($type);
$render = '_ctl' . ucfirst($type);
$attribs = (array)$attribs;

$__ctl_out = false;
if (method_exists($this, $render)) {
$__ctl_out = $this->{$render}($name, $attribs);
} else {
$extfilename = ucfirst($type) . '.php';
if (!isset($this->_extends[$type])) {
foreach ($this->_extendsDir as $dir) {
if (file_exists($dir . DS . $extfilename)) {
require($dir . DS . $extfilename);
$this->_extends[$type] = true;
break;
}
}
}

if (isset($this->_extends[$type])) {
$__ctl_out = call_user_func_array($render,
array('name' => $name, 'attribs' => $attribs));
}
}

if ($__ctl_out === false) {
$__ctl_out = "INVALID CONTROL TYPE \"{$type}\"";
}

if ($return) { return $__ctl_out; }
echo $__ctl_out;
return '';
}

实例化控件之后,在模版(也就是 .php)中:

1 <?php
2 $ui->control('textbox', 'username',
3 array(
4 'class' => 'textbox',
5 'size' => 28,
6 'maxlength' => 22,
7 )
8 );
9 ?>

如果使用smarty,调用方式就是:

{ webcontrol type='textbox' value=$textbox_value }
系统会自动去实例化控件对象

标签:,
comments Comments (318)    -
2011/08/15 at 16:08

PHP利用header函数实现各种状态的跳转

header实现301永久重定向
Header("HTTP/1.1 301 Moved Permanently");
Header("Location: http://www.zui88.com");

header实现302临时重定向
Header("HTTP/1.1 302 Found");
Header("Location: http://www.zui88.com");

header实现404无法找到页面
Header("HTTP/1.1 404 Not Found");

一下函数可实现各种状态的跳转:

/**
* 跳转页面
*
* 使用header()进行页面跳转,不显示任何内容.如果不能使用header跳转
* @param string $url
* @param int $status
*/
function goto($url,$status=null)
{
if(!empty($status))
{
$status=intval($status);
$codes = array(
100 => "Continue",
101 => "Switching Protocols",
200 => "OK",
201 => "Created",
202 => "Accepted",
203 => "Non-Authoritative Information",
204 => "No Content",
205 => "Reset Content",
206 => "Partial Content",
300 => "Multiple Choices",
301 => "Moved Permanently",
302 => "Found",
303 => "See Other",
304 => "Not Modified",
305 => "Use Proxy",
307 => "Temporary Redirect",
400 => "Bad Request",
401 => "Unauthorized",
402 => "Payment Required",
403 => "Forbidden",
404 => "Not Found",
405 => "Method Not Allowed",
406 => "Not Acceptable",
407 => "Proxy Authentication Required",
408 => "Request Time-out",
409 => "Conflict",
410 => "Gone",
411 => "Length Required",
412 => "Precondition Failed",
413 => "Request Entity Too Large",
414 => "Request-URI Too Large",
415 => "Unsupported Media Type",
416 => "Requested range not satisfiable",
417 => "Expectation Failed",
500 => "Internal Server Error",
501 => "Not Implemented",
502 => "Bad Gateway",
503 => "Service Unavailable",
504 => "Gateway Time-out"
);
if (array_key_exists($status,$codes)) {
$code = $status;
$msg = $codes[$status];
$status = "HTTP/1.1 {$code} {$msg}";
} else {
$status = null;
}
}
if (!empty($status)) {
header($status);
}
if(!empty($url)) {
$url=url($url);header("Location: $url");
if ($code==404) {
echo "<meta http-equiv='refresh' content='0;url=$url'>";
}
}
exit;
}

标签:,
comments Comments (250)    -
2011/08/15 at 09:58

HTML表单里面使用fieldset,legend标签,实现标题浮在边线上的效果

经常看到一些表单会有一种相同的特殊效果,就是表单说明文字会浮在边线上,可以用css控制实现,但是其实html有一个fieldset标签来实现这效果
HTML <fieldset> 标签

定义和用法

fieldset 元素可将表单内的相关元素分组。

<fieldset> 标签将表单内容的一部分打包,生成一组相关表单的字段。

当一组表单元素放到 <fieldset> 标签内时,浏览器会以特殊方式来显示它们,它们可能有特殊的边界、3D 效果,或者甚至可创建一个子表单来处理这些元素。

<fieldset> 标签没有必需的或唯一的属性。

<legend> 标签为 fieldset 元素定义标题。

实例

组合表单中的相关元素:

<form>

<fieldset>

<legend>health information</legend>

height: <input type="text" />

weight: <input type="text" />

</fieldset>

</form>
实现效果如下

health information

height:

weight:

还可以用css控制边线的样式和legend的样式。

<form>

<fieldset>

<legend>health information</legend>

height: <input type="text" />

weight: <input type="text" />

</fieldset>

</form>

标签:,
comments Comments (293)    -
2011/08/15 at 01:19

php正则匹配时常用到的模式修正符

PHP简单的模版引擎主要的原理是使用preg_replace对模板的内容进行替换,替换成可执行的php语句然后写入缓存文本,再include进来执行。

在使用preg_replace时常用到的模式修正符有:

e:$replacement 的字符串将被当作php语句执行

U:禁止贪婪匹配 只跟踪到最近的一个匹配符并结束,

m:在匹配首内容或者尾内容时候采用多行识别匹配

s:模式中的圆点元字符(.)匹配所有的字符,包括换行

标签:,
comments Comments (267)    -