php随机生成字符串能够团结定义自个儿所急需的尺寸,在实际应用开荒中,平时遇上。
自便生成数数字
正文实例陈说了php截取html字符串及活动补全html标签的议程。分享给咱们供大家仿照效法。具体剖判如下:
和讯的解说有字数限制,其计数形式是,汉语算2个,立陶宛(Lithuania)语算1个,全角字符算2个,半角字符算1个。
php中自带strlen是回来的字节数,对于utf8编码的国语再次来到时3个,不满足需要。
mb_strlen
能够依照字符集总括长度,比方utf8的汉语计数为1,但那不符合新浪字数限制须要,汉语必须总结为2才得以。
google了下,找到二个discuz中截取各类编码字符的类,改换了下,已经测量检验通过.个中参数$charset
只帮忙gbk与utf-8。
复制代码 代码如下:
mt_rand()函数
此处计算一下关于采用php截取html字符串自动补全html标签,实际支出中会日常蒙受,相当多个凡尘接先strip_tags过滤掉html标签,可是就只剩余纯文本了,可读性很差,上边是二个函数,代码如下:
复制代码 代码如下:
//随机生成字符串
function random($length) {
srand(date(“s”));
$possible_charactors = “0123456789ABCDEFGHIJKLMNOPQRSTUVWXYZ”;
$string = “”;
while(strlen($string)<$length) {
$string .=
substr($possible_charactors,(rand()%(strlen($possible_charactors))),1);
}
return($string);
}
代码如下
复制代码 代码如下:
$a = “s@@你好”;
var_dump(strlen_weibo($a,’utf-8′));
您恐怕感兴趣的篇章:
- PHP生成自由字符串(3种方法)
- PHP随机字符串生成代码(包含大小写字母)
- php生成随机数可能字符串的代码
- PHP 三个私行字符串生成代码
- 转换随机字符串和验证码的类的PHP实例
- PHP系列号生成函数和字符串替换函数代码
- PHP生成钦赐随机字符串的简便落成方式
- php生成自由字符串可钦点纯数字、纯字母只怕夹杂的
- php完结基于字符串生成对应数组的方式
- php生出自由字符串
$num = mt_rand(0,9999999);
/**
* 截取HTML,并机关补全闭合
* @param $html
* @param $length
* @param $end
*/
function subHtml($html,$length) {
$result = ”;
金沙澳门官网7817网址, $tagStack = array();
$len = 0;
$contents = preg_split(“~(<[^>]+?>)~si”,$html,
-1,PREG_SPLIT_NO_EMPTY| PREG_SPLIT_DELIM_CAPTURE);
foreach($contents as $tag)
{
if (trim($tag)==””) continue;
if(preg_match(“~<([a-z0-9]+)[^/>]*?/>~si”,$tag)){
$result .= $tag;
}else
if(preg_match(“~</([a-z0-9]+)[^/>]*?>~si”,$tag,$match)){
if($tagStack[count($tagStack)-1] == $match[1]){
array_pop($tagStack);
$result .= $tag;
}
}else
if(preg_match(“~<([a-z0-9]+)[^/>]*?>~si”,$tag,$match)){
array_push($tagStack,$match[1]);
$result .= $tag;
}else if(preg_match(“~<!–.*?–>~si”,$tag)){
$result .= $tag;
}else{
if($len + mstrlen($tag) < $length){
$result .= $tag;
$len += mstrlen($tag);
}else {
$str = msubstr($tag,0,$length-$len+1);
$result .= $str;
break;
}
}
}
while(!emptyempty($tagStack)){
$result .= ‘</’.array_pop($tagStack).’>’;
}
return $result;
}
/**
* 截取中文字符串
* @param $string 字符串
* @param $start 起始位
* @param $length 长度
* @param $charset 编码
* @param $dot 附加字串
*/
function msubstr($string, $start, $length,$dot=”,$charset = ‘UTF-8’)
{
$string = str_replace(array(‘&’, ‘”‘, ‘<‘, ‘>’,’ ‘), array(‘&’,
‘”‘, ‘<‘, ‘>’,’ ‘), $string);
if(strlen($string) <= $length) {
return $string;
}
if(strtolower($charset) == ‘utf-8’) {
$n = $tn = $noc = 0;
while($n < strlen($string)) {
$t = ord($string[$n]);
if($t == 9 || $t == 10 || (32 <= $t && $t <= 126)) {
$tn = 1; $n++;
} elseif(194 <= $t && $t <= 223) {
$tn = 2; $n += 2;
} elseif(224 <= $t && $t <= 239) {
$tn = 3; $n += 3;
} elseif(240 <= $t && $t <= 247) {
$tn = 4; $n += 4;
} elseif(248 <= $t && $t <= 251) {
$tn = 5; $n += 5;
} elseif($t == 252 || $t == 253) {
$tn = 6; $n += 6;
} else {
$n++;
}
$noc++;
if($noc >= $length) {
break;
}
}
if($noc > $length) {
$n -= $tn;
}
$strcut = substr($string, 0, $n);
} else {
for($i = 0; $i < $length; $i++) {
$strcut .= ord($string[$i]) > 127 ? $string[$i].$string[++$i]
: $string[$i];
}
}
return $strcut.$dot;
}
/**
* 获得字符串的长短,包蕴中菲律宾语。
*/
function mstrlen($str,$charset = ‘UTF-8’){
if (function_exists(‘mb_substr’)) {
$length=mb_strlen($str,$charset);
} elseif (function_exists(‘iconv_substr’)) {
$length=iconv_strlen($str,$charset);
} else {
preg_match_all(“/[x01-x7f]|[xc2-xdf][x80-xbf]|xe0[xa0-xbf][x80-xbf]|[xe1-xef][x80-xbf][x80-xbf]|xf0[x90-xbf][x80-xbf][x80-xbf]|[xf1-xf7][x80-xbf][x80-xbf][x80-xbf]/”,
$text, $ar);
$length=count($ar[0]);
}
return $length;
}
结果输出为8,当中字母s计数为1,全角@计数为2,半角@计数为1,五个中文计数为4。源码如下: