对一段文字按照字数进行分割,因为文字中可能是中英文混合的,而php函数strlen只能计算出字串的字节数,于是自己实现了几个函数,分享下。
例1,计算字符总长度。
<?php
function ccStrLen($str) #计算中英文混合字符串的长度
{
$ccLen=0;
$ascLen=strlen($str);
$ind=0;
$hasCC=ereg(”[xA1-xFE]“,$str); #判断是否有汉字
$hasAsc=ereg(”[x01-xA0]“,$str); #判断是否有ASCII字符
if($hasCC && !$hasAsc) #只有汉字的情况
return strlen($str)/2;
if(!$hasCC && $hasAsc) #只有Ascii字符的情况
return strlen($str);
for($ind=0;$ind<$ascLen;$ind++)
{
if(ord(substr($str,$ind,1))>0xa0)
{
$ccLen++;
$ind++;
}
else
{
$ccLen++;
}
}
return $ccLen;
}
?>
例2,从左侧截取字符串。
<?php
function ccStrLeft($str,$len) #从左边截取中英文混合字符串
{
$ascLen=strlen($str); if($ascLen<=$len) return $str;
$hasCC=ereg(”[xA1-xFE]“,$str); #同上
$hasAsc=ereg(”[x01-xA0]“,$str);
if(!$hasCC) return substr($str,0,$len);
if(!$hasAsc)
if($len & 0×01) #如果长度是奇数
return substr($str,0,$len+$len-2);
else
return substr($str,0,$len+$len);
$cind=0;$flag=0;$reallen=0;//实际取字节长
while($cind<$ascLen && $reallen<$len)
{ //by www.
if(ord(substr($str,$cind,1))<0xA1){ //如果该字节为英文 则加一
$cind++;
}else{//否则 加2个字节
$cind+=2;
}
$reallen++;
}
return substr($str,0,$cind);
}
?>
例3,把给定文字,按切割数量存入数组(适合短篇文字,长文章可没分隔一部分就直接处理一次)
<?php
function SplitContent($content,$smslen){
$str_tmp=$content;
$arr_cont=array();
$len_tmp=0;
$i=0;//分割绝对位置
while (strlen($str_tmp)>0){
$str_tmp=ccStrLeft($str_tmp,$smslen);
array_push($arr_cont,$str_tmp);
$i+=strlen($str_tmp);
$str_tmp=substr($content,$i,strlen($content));
}
return $arr_cont;
} //by www.
?>
测试:
<?php $str=’a计算中英文混合1234字符串的长度abcd’; echo $str.’的长度为:’.ccStrLen($str); echo ‘<br>’; $smslen=3;//截取长度 print_r(SplitContent($str,$smslen)); ?>
分割结果:
Array
(
[0] => a计算
[1] => 中英文
[2] => 混合1
[3] => 234
[4] => 字符串
[5] => 的长度
[6] => abc
[7] => d
)
有关php中$_REQUEST的用法,很多资料上都有介绍。
在php手册中,这个变量解释为:"默认情况下包含了 $_GET,$_POST 和 $_COOKIE 的数组。"
注意其中包含cookie的内容,做个测试:
<?php $_COOKIE['name'] = "aaa"; print_r($_REQUEST); ?>
输出结果:
Array ( )
并没有取到cookie的内容呀,为什么呢?
再看手册,下面有一行:
5.3.0 引入 request_order。该指令会影响 $_REQUEST 的内容
我的php版本是5.3.17,打开php.ini,找到request_order:
; be registered into the super global array REQUEST. If so, it also determines
; the order in which that data is registered. The values for this directive are
; specified in the same manner as the variables_order directive, EXCEPT one.
; Leaving this value empty will cause PHP to use the value set in the
; variables_order directive. It does not mean it will leave the super globals
; array REQUEST empty.
; Default Value: None
; Development Value: "GP"
; Production Value: "GP"
; http://php.net/request-order
request_order = "GP"
翻译:
这条指令确定了哪些超全局数据该被注册到超全局数组REQUEST中,这些超全局数据包括G(GET),P(POST),C(COOKIE),E(ENV),S(SERVER)。这条指令同样指定了这些数据的注册顺序,换句话说GP和PG是不一样的。注册的顺序是从左至右,即右侧的值会覆盖左侧的。比如,当设置为GPC时,COOKIE > POST > GET,依次覆盖。如果这个项被设置为空,php将会使用指令variables_order的值来指定。
如果将php.ini中的request_order设置为"GPC"后,运行下面的程序post.php,看看结果如何:
<?php
error_reporting(0);
setcookie("value", "cookie", time()+3600);
echo "===GET===<br />";
echo $_GET['value'] . "<br />";
echo "===POST===<br />";
echo $_POST['value'] . "<br />";
echo "===COOKIE===<br />";
echo $_COOKIE['value'] . "<br />";
echo "===REQUEST===<br />";
echo $_REQUEST['value'] . "<br />";
?>
<form method="post" action="/blog_article/value/get.html">
<input type="text" name="value" value="post"/>
<input type="submit" value="submit"/>
</form>
输出结果:
get
===POST===
post
===COOKIE===
cookie
===REQUEST===
cookie
可以看到cookie的值被取到了。
为了使程序兼容更多的版本,不要在程序中使用$_REQUEST这个超全局变量。
当程序确实需要接收get和post过来的参数时,可以用下面的方法代替:
$_REQ = array_merge($_GET, $_POST);
完整代码:
<?php
error_reporting(0);
setcookie("value", "cookie", time()+3600);
$_REQ = array_merge($_GET, $_POST);
echo "===GET===<br />";
echo $_GET['value'] . "<br />";
echo "===POST===<br />";
echo $_POST['value'] . "<br />";
echo "===COOKIE===<br />";
echo $_COOKIE['value'] . "<br />";
echo "===REQUEST===<br />";
echo $_REQ['value'] . "<br />";
?>
<form method="post" action="/blog_article/value/get.html">
<input type="text" name="value" value="post"/>
<input type="submit" value="submit"/>
</form>
输出结果:
get
===POST===
post
===COOKIE===
cookie
===REQUEST===
post
另外,判断是post还是get请求的页面,最好使用:
方法。
有关php数组全排列的递归算法的代码,参见文章:php全排列的递归算法的代码 。
以下是php数组全排例的非递归算法实现代码:
<?php
/**
* 取得数组的全排列
*
* @param array $source 待排列数组,一维
* @return array
* @site www.
*/
function getAllPerm($source)
{
$rs = array();
sort($source);
$last = count($source) - 1;
$z = 0;
$x = $last;
$rs[] = $source;
while($x > 0)
{
// 相邻的两个元素,先将x的值赋给y,x再自减1
$y = $x--;
// 如果前一个元素的值小于后一个元素的值
if($source[$x] < $source[$y])
{
// 从尾部开始,找到第一个大于 $x 元素的值
$z = $last;
while($source[$x] > $source[$z])
{
$z--;
}
// 交换 $x 和 $z 元素的值
list($source[$x], $source[$z]) = array($source[$z], $source[$x]);
// 将 $y 之后的元素全部逆向排列
for($i = $last; $i > $y; $i--, $y++)
{
list($source[$i], $source[$y]) = array($source[$y], $source[$i]);
}
$rs[] = $source;
$x = $last;
}
}
return $rs;
}
$source = array(1,2,3);
$rs = getAllPerm($source);
print_r($rs);
?>
输出结果:
Array
(
[0] => Array
(
[0] => 1
[1] => 2
[2] => 3
)
[1] => Array
(
[0] => 1
[1] => 3
[2] => 2
)
[2] => Array
(
[0] => 2
[1] => 1
[2] => 3
)
[3] => Array
(
[0] => 2
[1] => 3
[2] => 1
)
[4] => Array
(
[0] => 3
[1] => 1
[2] => 2
)
[5] => Array
(
[0] => 3
[1] => 2
[2] => 1
)
)