php取整函数大分析 不指定

jed , 2008-11-27 22:24 , 代码编程 , 评论(0) , 阅读(2593) , Via 本站原创
经常用到取整的函数,今天小小的总结一下!其实很简单,就是几个函数而已~~主要是:ceil,floor,round,intval

ceil -- 进一法取整
说明
float ceil ( float value )
返回不小于 value 的下一个整数,value 如果有小数部分则进一位。ceil() 返回的类型仍然是 float,因为 float 值的范围通常比 integer 要大。

例子 1. ceil() 例子

echo ceil(4.3);     // 5
echo ceil(9.999);     // 10
?>  


floor -- 舍去法取整
说明
float floor ( float value )
返回不大于 value 的下一个整数,将 value 的小数部分舍去取整。floor() 返回的类型仍然是 float,因为 float 值的范围通常比 integer 要大。
Tags:
1.时间格式
{dede:field name='pubdate' function='strftime("%Y年%m月%d日 %H:%M:%S","@me")' /}2007年1月1日 18:30:02
{dede:field name='pubdate' function='strftime("%Y-%m-%d %H:%M:%S","@me")' /}2007-1-1 18:30:02
{dede:field name='pubdate' function='strftime("%Y年%m月%d日 %H时%M分%S秒","@me")' /}2007年1月1日 18时30分02秒
{dede:field name='pubdate' function='strftime("%m-%d %H:%M:%S","@me")' /}1-1 18:30:02
{dede:field name='pubdate' function='strftime("%m-%d","@me")' /}1-1
%Y-年
%m-月
%d-日
%H-小时
%M-分
%S-秒
2.24小时内的时间显示红色..
[field:pubdate runphp='yes']
$a="<font color='#ff0000'>";
$b="</font>";
$c=strftime("%Y年%m月%d日 %H:%M:%S","@me");
$ntime = time();
$oneday = 3600 * 24;
if(($ntime - @me)<$oneday) @me = $a.$c.$b;
else @me =$c;
[/field:pubdate]
3.最后更新时间
最后更新时间:{dede:tagname runphp='yes'}@me = date("Y-m-d H:i:s", time());{/dede:tagname}
4.XX天前(21楼kofkof提供)
[field:pubdate runphp='yes']
$today = Floor(time()/(3600 * 24));
$senday= Floor(@me/(3600 * 24));
$updays = $today-$senday;
if($updays==0) @me = "今日";
else @me = $updays."天前";
[/field:pubdate]


{dede:arclist row=11 titlelen=24 orderby=pubdate}

[[field:typelink function='str_replace("a ","a class=ulink ",@me)'/]]

[field:textlink/]

[field:senddate function="strftime('%m-%d',@me)"/]

{/dede:arclist}
栏目 标题 时间
在utf-8编码的情况下,首字母可能是中文或者英文,英文排在前头,中文排在后面,中文需要按照拼音顺序排序,请问如何实现?
如果是GBK编码的,可以直接对字段进行order by ,但是纯utf-8环境下不行。于是在网上找到了如下两种方法,我试的是第二种,还不错。

Ruby代码


conv = Iconv.new("GBK", "utf-8")  




mysql:

Sql代码

SELECT * FROM table ORDER BY CONVERT( NAME  USING gbk ) ;
Tags: ,
因为我的记忆力严重不足,所以这几天写了一个UTF-8的备忘录,结果发现在UTF-8的编码下,mysql不能正确地排序,于是换到php里面用sort,继续失败……汗死……后来一想,可能是因为UTF-8的编码不是按照拼音来编码的,所以才会导致这个原因,另外貌似GBK的字库也不是完全按拼音排序的,"窦"字在GBK里面存在,但是却排在最后面,为了避免错乱,故在排序时把"窦"替换成同音字"豆",即可,于是写了这个排序函数……哎,弄了我一个晚上,结果就8行代码……无语了……

PHP代码

function name_cmp($a, $b) {    
$a = str_replace('窦', '豆', $a);    
$b = str_replace('窦', '豆', $b);    
$a = iconv('UTF-8', 'GBK', $a);    
$b = iconv('UTF-8', 'GBK', $b);    
$a = ereg_replace('^(a|an|the) ', '', strtolower($a));    
$b = ereg_replace('^(a|an|the) ', '', strtolower($b));    
return strcasecmp($a, $b);    
}    
  
usort($array, 'name_cmp');    

关于网上的mysql按拼音查询的两种说法:
1. 用ORDER BY BINARY(字段) ASC的方式查询数据库
    实际上在纯UTF-8的环境下是实现不了了,只有用UTF-8的编码去保存GBK的字符之后才能这样使用,而且不能忘记替换像"窦"这种特殊的字哦……

2. 在数据库中新建一个字段,保存汉字拼音,再利用该字段查询
    这个方法在数据库里面看起来比较直观,不过同样需要把汉字转换成拼音,直接获得拼音的首字母还是不够的.......
    在网上找了份代码,运行的还不错,不过需要下载一个字库文件:
  

RAR压缩包:
下载该附件:http://www.bigasp.com.cn/Uploads/Files/Code/gbk拼音字库.rar

    预览地址:http://www.bigasp.com.cn/extra/gbk2py
    代码如下:

PHP代码

class my_Getpy {    
var $_dat = 'py.dat';    
var $_fd  = false;    
  
function my_Getpy($pdat = '') {    
  if ('' != $pdat)    
  $this->_dat = $pdat;    
}    
  
function load($pdat = '') {    
  if ('' == $pdat)    
  $pdat = $this->_dat;    
  $this->unload();    
  $this->_fd = @fopen($pdat, 'rb');    
  if (!$this->_fd) {    
   trigger_error("unable to load PinYin data file `$pdat`", E_USER_WARNING);    
   return false;    
  }    
  return true;    
}    
  
function unload() {    
  if ($this->_fd) {    
   @fclose($this->_fd);    
   $this->_fd = false;    
  }    
}    
  
function get($zh) {    
  if (strlen($zh) != 2) {    
   trigger_error("`$zh` is not a valid GBK hanzi", E_USER_WARNING);    
   return false;    
  }    
  if (!$this->_fd && !$this->load()) return false;    
  $high = ord($zh[0]) - 0x81;    
  $low  = ord($zh[1]) - 0x40;    
  // 计算偏移位置    
  $nz = ($ord0 - 0x81);    
  $off = ($high<<8) + $low - ($high * 0x40);    
  // 判断 off 值    
  if ($off < 0) {    
   trigger_error("`$zh` is not a valid GBK hanzi-2", E_USER_WARNING);    
   return false;    
  }    
  fseek($this->_fd, $off * 8, SEEK_SET);    
  $ret = fread($this->_fd, 8);    
  $ret = unpack('a8py', $ret);    
  return $ret['py'];    
}    
  
function _my_Getpy() {    
  $this->_unload();    
}    
}
// demo 测试例子  
?>    
  
<title>GBK码汉字转拼音</title>    
<h1>GBK码汉字转拼音</h1>    
  
<form method=get>    
输入汉字试试:<input type="text" size="16" name="zh">    
<input type="submit">    
</form>    
  
<?php    
if ($str = $_GET['zh']) {    
$py = new my_Getpy;        
$len = strlen($str);    
$ret = '';    
for ($i = 0; $i < $len; $i++) {    
  if (ord($str[$i]) > 0x80) {    
   $xx = $py->get(substr($str, $i, 2));    
   $ret .= ($xx ?  $xx . ' ' : substr($str, $i, 2));        
   $i++;    
  } else {    
   $ret .= $str[$i];    
  }    
}    
$py->unload();    
echo "字串 `<font color=red>{$str}</font>` 的拼音是: <font color=red>{$ret}</font>\n";    
}    
?>    
Tags: ,
js对文字进行编码涉及3个函数:escape,encodeURI,encodeURIComponent,相应3个解码函数:unescape,decodeURI,decodeURIComponent

1、  传递参数时需要使用encodeURIComponent,这样组合的url才不会被#等特殊字符截断。                            

例如:



<script language="javascript">document.write('<a href="http://passport.baidu.com/?logout&aid=7&u='+encodeURIComponent("http://cang.baidu.com/bruce42")+'">退出</a>');</script>



2、  进行url跳转时可以整体使用encodeURI

例如: Location.href=encodeURI(http://cang.baidu.com/do/s?word=中国asp之家&ct=21);
3、  js使用数据时可以使用escape

例如:搜藏中history纪录。

4、  escape对0-255以外的unicode值进行编码时输出%u****格式,其它情况下escape,encodeURI,encodeURIComponent编码结果相同。

最多使用的应为encodeURIComponent,它是将中文、韩文等特殊字符转换成utf-8格式的url编码,所以如果给后台传递参数需要使用encodeURIComponent时需要后台解码对utf-8支持(form中的编码方式和当前页面编码方式相同)

(中国asp之家注:就是说如果你的页面编码是gb2312的话,服务器端接收的将是乱码)

escape不编码字符有69个:*,+,-,.,/,@,_,0-9,a-z,A-Z

encodeURI不编码字符有82个:!,#,$,&,',(,),*,+,,,-,.,/,:,;,=,?,@,_,~,0-9,a-z,A-Z

encodeURIComponent不编码字符有71个:!, ',(,),*,-,.,_,~,0-9,a-z,A-Z

附上这个三个函数的介绍:

escape 方法

对 String 对象编码以便它们能在所有计算机上可读,

escape(charString)
必选项 charstring 参数是要编码的任意 String 对象或文字。

说明
escape 方法返回一个包含了 charstring 内容的字符串值( Unicode 格式)。所有空格、标点、重音符号以及其他非 ASCII 字符都用 %xx 编码代替,其中 xx 等于表示该字符的十六进制数。例如,空格返回的是 "%20" 。

字符值大于 255 的以 %uxxxx 格式存储。

注意   escape 方法不能够用来对统一资源标示码 (URI) 进行编码。对其编码应使用 encodeURI 和encodeURIComponent 方法。

encodeURI 方法

将文本字符串编码为一个有效的统一资源标识符 (URI)。

encodeURI(URIString)
必选的 URIString 参数代表一个已编码的 URI。

说明
encodeURI 方法返回一个编码的 URI。如果您将编码结果传递给 decodeURI,那么将返回初始的字符串。encodeURI 方法不会对下列字符进行编码:":"、"/"、";" 和 "?"。请使用 encodeURIComponent 方法对这些字符进行编码。

encodeURIComponent 方法

将文本字符串编码为一个统一资源标识符 (URI) 的一个有效组件。

encodeURIComponent(encodedURIString)
必选的 encodedURIString 参数代表一个已编码的 URI 组件。

说明

encodeURIComponent 方法返回一个已编码的 URI。如果您将编码结果传递给 decodeURIComponent,那么将返回初始的字符串。因为 encodeURIComponent 方法对所有的字符编码,请注意,如果该字符串代表一个路径,例如 /folder1/folder2/default.html,其中的斜杠也将被编码。这样一来,当该编码结果被作为请求发送到 web 服务器时将是无效的。如果字符串中包含不止一个 URI 组件,请使用 encodeURI 方法进行

Tags: , ,
escape() 方法:
采用ISO Latin字符集对指定的字符串进行编码。所有的空格符、标点符号、特殊字符以及其他非ASCII字符都将被转化成%xx格式的字符编码(xx等于该字符在字符集表里面的编码的16进制数字)。比如,空格符对应的编码是%20。unescape方法与此相反。不会被此方法编码的字符: @ * / +
英文解释:MSDN JScript Reference: The escape method returns a string value (in Unicode format) that contains the contents of [the argument]. All spaces, punctuation, accented characters, and any other non-ASCII characters are replaced with %xx encoding, where xx is equivalent to the hexadecimal number representing the character. For example, a space is returned as "%20."
Edge Core Javascript Guide: The escape and unescape functions let you encode and decode strings. The escape function returns the hexadecimal encoding of an argument in the ISO Latin character set. The unescape function returns the ASCII string for the specified hexadecimal encoding value.

encodeURI() 方法:把URI字符串采用UTF-8编码格式转化成escape格式的字符串。不会被此方法编码的字符:! @ # $& * ( ) = : / ; ? + '
英文解释:MSDN JScript Reference: The encodeURI method returns an encoded URI. If you pass the result to decodeURI, the original string is returned. The encodeURI method does not encode the following characters: ":", "/", ";", and "?". Use encodeURIComponent to encode these characters. Edge Core Javascript Guide: Encodes a Uniform Resource Identifier (URI) by replacing each instance of certain characters by one, two, or three escape sequences representing the UTF-8 encoding of the character

encodeURIComponent() 方法:把URI字符串采用UTF-8编码格式转化成escape格式的字符串。与encodeURI()相比,这个方法将对更多的字符进行编码,比如 / 等字符。所以如果字符串里面包含了URI的几个部分的话,不能用这个方法来进行编码,否则 / 字符被编码之后URL将显示错误。不会被此方法编码的字符:! * ( )
英文解释:MSDN JScript Reference: The encodeURIComponent method returns an encoded URI. If you pass the result to decodeURIComponent, the original string is returned. Because the encodeURIComponent method encodes all characters, be careful if the string represents a path such as /folder1/folder2/default.html. The slash characters will be encoded and will not be valid if sent as a request to a web server. Use the encodeURI method if the string contains more than a single URI component. Mozilla Developer Core Javascript Guide: Encodes a Uniform Resource Identifier (URI) component by replacing each instance of certain characters by one, two, or three escape sequences representing the UTF-8 encoding of the character.

因此,对于中文字符串来说,如果不希望把字符串编码格式转化成UTF-8格式的(比如原页面和目标页面的charset是一致的时候),只需要使用escape。如果你的页面是GB2312或者其他的编码,而接受参数的页面是UTF-8编码的,就要采用encodeURI或者encodeURIComponent。

另外,encodeURI/encodeURIComponent是在javascript1.5之后引进的,escape则在javascript1.0版本就有。

英文注释:The escape() method does not encode the + character which is interpreted as a space on the server side as well as generated by forms with spaces in their fields. Due to this shortcoming, you should avoid use of escape() whenever possible. The best alternative is usually encodeURIComponent().Use of the encodeURI() method is a bit more specialized than escape() in that it encodes for URIs [REF] as opposed to the querystring, which is part of a URL. Use this method when you need to encode a string to be used for any resource that uses URIs and needs certain characters to remain un-encoded. Note that this method does not encode the ' character, as it is a valid character within URIs.Lastly, the encodeURIComponent() method should be used in most cases when encoding a single component of a URI. This method will encode certain chars that would normally be recognized as special chars for URIs so that many components may be included. Note that this method does not encode the ' character, as it is a valid character within URIs.


Tags: , ,

js替换应用 不指定

jed , 2008-9-17 11:29 , 代码编程 , 评论(0) , 阅读(5316) , Via 本站原创


<script language="javascript">
var str="</SheetItems><ElectricalStatus>";
str=str.replace(/\</g,"!");
alert(str);
</script>

Tags:
显然:UL+LI在布局上比table灵活得多。

比较分两列或者多列显示:

table:你需要在服务器上写一大堆代码判断是不是需要换行,或者切分是不是准确

DIV:你只需要定义好css,剩下的做事情叫交给浏览器吧。

最简单的代码:



<style type="text/css">  
    .mycode{ width:300px; height:74px; float:left;}    
    .mycode ul{ width:280px;}    
    .mycode li{ width:100px; float:left; display:block;}    
</style>  
<div class="mycode">  
    <ul>  
        <li>博客小子</li>  
        <li>博客小子</li>  
        <li>博客小子</li>  
        <li>博客小子</li>  
        <li>博客小子</li>  
        <li>博客小子</li>  
    </ul>  
</div>  




不用多说,这个是最精简的代码,请根据你需要修改!
Tags:
公司需要做一个本周排行榜,想想得通过当前日期,得到本周开始日期和结束日期。自己写了个类,就不公布上来了,一个笨办法,后来经高人指点,原来可以如此简单的获得,汗!!!与大家分享:



$date1   =   date("Y-m-d",strtotime("Sunday"));   //周开始  
$date2   =   date("Y-m-d",strtotime("Saturday"));   //周结束
$date1   =   date("Y-m-d",strtotime("last   Sunday"));   //上周开始  
$date2   =   date("Y-m-d",strtotime("last   Saturday"));   //上周结束


Tags:

搞定CSS SPAN和DIV的区别 不指定

jed , 2008-9-3 15:31 , 代码编程 , 评论(1) , 阅读(6019) , Via 本站原创
  SPAN 和 DIV 的区别在于,DIV(division)是一个块级元素,可以包含段落、标题、表格,乃至诸如章节、摘要和备注等。而SPAN 是行内元素,SPAN 的前后是不会换行的,它没有结构的意义,纯粹是应用样式,当其他行内元素都不合适时,可以使用SPAN。

  下面以一个实例来说明这两个属性的区别。

  代码:

  SPAN标记有一个重要而实用的特性,即它什么事也不会做,它的唯一目的就是围绕你的HTML代码中的其它元素,这样你就可以为它们指定样式了。在此例中,标识符允许你将一个段落分成不同的部分。

  还有一个标识符具有类似的功能,

DIV也被用来在HTML文件中建立逻辑部分。但与
SPAN不同,
工作于文本块一级,它在它所包含的HTML元素的前面及后面都引入了行分隔。

  效果:

  SPAN标记有一个重要而实用的特性,即它什么事也不会做,它的唯一目的就是围绕你的HTML代码中的其它元素,这样你就可以为它们指定样式了。在此例中,标识符允许你将一个段落分成不同的部分。

  还有一个标识符具有类似的功能,

  DIV也被用来在HTML文件中建立逻辑部分。但与

  SPAN不同,

  工作于文本块一级,它在它所包含的HTML元素的前面及后面都引入了行分隔。

Tags:
分页: 11/26 第一页 上页 6 7 8 9 10 11 12 13 14 15 下页 最后页 [ 显示模式: 摘要 | 列表 ]