100分求个 检测字符串长度 本帖最后由 lq1614 于 2012-07-03 14:10:37 编辑 解决方案 » 免费领取超大流量手机卡,每月29元包185G流量+100分钟通话, 中国电信官方发货 我没太看懂楼主的题,不过函数,手头上有一个。/** * 得到utf8 的长度 * @author mu_rain * @param String $str * @return Int */ static function strlen_utf8($str) { $i = 0; $count = 0; $len = strlen ($str); while ($i < $len) { $chr = ord ($str[$i]); $count++; $i++; if($i >= $len) break; if($chr & 0x80) { $chr <<= 1; while ($chr & 0x80) { $i++; $chr <<= 1; } } } return $count; } function myLen($str,$startTag='[',$endTag=']',$encoding='utf-8') { $st = preg_quote($startTag); $et = preg_quote($endTag); return mb_strlen(preg_replace("#{$st}[^{$et}]*{$et}#","~",$str),$encoding); }echo myLen("123asd你好[哈哈]");//9 $str[0] = "123asd你好[哈哈]"[哈哈] == 1个字符$str[0] 结果是九个字符 附赠字符串函数一堆 ,慢用。<?php/** * Created by JetBrains PhpStorm. * User: Administrator * Date: 12-2-17 * Time: 上午11:43 * To change this template use File | Settings | File Templates. *//** * 字符串处理基类. * @author mu_rain */class kString{ // ------------------------------------------------------------------------ /** * replace the base64_encode to encode the string * * @param $str 对象的实例 * @package KDG * @subpackage String * @category Putils * @author jim * @return mixed */ // ------------------------------------------------------------------------ public static function encode($str){ $src = array("/","+","="); $dist = array("-a","-b","-c"); $old = base64_encode($str); $new = str_replace($src,$dist,$old); return $new; } // ------------------------------------------------------------------------ /** * replace the base64_decode to decode the string * * @param $str 对象的实例 * @package P * @subpackage String * @category Putils * @author jim * @return mixed */ // ------------------------------------------------------------------------ public static function decode($str){ $src = array("-a","-b","-c"); $dist = array("/","+","="); $old = str_replace($src,$dist,$str); $new = base64_decode($old); return $new; } // ------------------------------------------------------------------------ /** * replace the base64_decode to decode the string * * @param $str 对象的实例 * @package P * @subpackage String * @category Putils * @author jim * @return mixed */ // ------------------------------------------------------------------------ public static function showAsFileSize($str){ // change 1024 to 1k $count = intVal($str); $destSize = $count/1024; $destSize = round($destSize,1); return $destSize."K"; } } // ------------------------------------------------------------------------ /** * regulary show the string or object or json. * 规格化显示 * * @param $str 对象的实例 * @package P * @subpackage String * @category Putils * @author jim * @return mixed */ // ------------------------------------------------------------------------ public static function pr($array,$title = 'DEBUG',$type = 'array') { $title .= date("Y-m-d H:i:s"); echo "<fieldset style=\"border: 1px solid rgb(0, 153, 0); margin: 20px 0pt; padding: 6px 10px 10px; background-color: rgb(238, 238, 238);\"><legend style=\"color: rgb(0, 153, 0);\">$title</legend>"; echo "<div style = 'font-size:14px; color:#000; border:1px solid #666; background:#ccc; padding:5px;'>"; print("<pre>"); if($type == 'json') { $array = json_decode($array); } print_r($array); print("</pre>"); echo "<div>"; echo "</fieldset>"; } // ------------------------------------------------------------------------ /** * * 去掉html标记。 * strip the html * @param unknown_type $string */ // ------------------------------------------------------------------------ public static function stripHtml($string){ $search = array ("'<script[^>]*?>.*?</script>'si", // 去掉 javascript 4B+1ZsmMd "'<[/!]*?[^<>]*?>'si", // 去掉 HTML 标记 "'([rn])[s]+'", // 去掉空白字符 "'&(quot|#34);'i", // 替换 HTML 实体 "'&(amp|#38);'i", "'&(lt|#60);'i", "'&(gt|#62);'i", "'&(nbsp|#160);'i", "'&(iexcl|#161);'i", "'&(cent|#162);'i", "'&(pound|#163);'i", "'&(copy|#169);'i", "'&#(d+);'e"); $replace = array( "", "", "\1", "\"", "&", "<", ">", " ", chr(161), chr(162), chr(163), chr(169), "chr(\1)"); return preg_replace($search, $replace, $string); } // ------------------------------------------------------------------------ /** * * 求子字符串 * @param unknown_type $str * @param unknown_type $start * @param unknown_type $lenth */ // -------------------------------------------------------------------------+ public static function subStringUtf8($str, $start, $lenth) { $len = strlen($str); $r = array (); $n = 0; $m = 0; for ($i = 0; $i < $len; $i++) { $x = substr($str, $i, 1); $a = base_convert(ord($x), 10, 2); $a = substr('00000000' . $a, -8); if ($n < $start) { if (substr($a, 0, 1) == 0) { } elseif (substr($a, 0, 3) == 110) { $i += 1; } elseif (substr($a, 0, 4) == 1110) { $i += 2; } $n++; } else { if (substr($a, 0, 1) == 0) { $r[] = substr($str, $i, 1); } elseif (substr($a, 0, 3) == 110) { $r[] = substr($str, $i, 2); $i += 1; } elseif (substr($a, 0, 4) == 1110) { $r[] = substr($str, $i, 3); $i += 2; } else { $r[] = ''; } if (++ $m >= $lenth) { break; } } } return join("", $r); } public static function readOnly($val) { if($val) return "readonly = 'readonly' style = 'color:gray; border:1px solid gray;'"; } public static function highlightCode($str) { // The highlight string function encodes and highlights // brackets so we need them to start raw $str = str_replace(array('<', '>'), array('<', '>'), $str); // Replace any existing PHP tags to temporary ers so they don't accidentally // break the string out of PHP, and thus, thwart the highlighting. $str = str_replace(array('<?', '?>', '<%', '%>', '\\', '</script>'), array('phptagopen', 'phptagclose', 'asptagopen', 'asptagclose', 'backslashtmp', 'scriptclose'), $str); // The highlight_string function requires that the text be surrounded // by PHP tags, which we will remove later $str = '<?php '.$str.' ?>'; // <? // All the magic happens here, baby! $str = highlight_string($str, TRUE); // Prior to PHP 5, the highligh function used icky <font> tags // so we'll replace them with <span> tags. if (abs(PHP_VERSION) < 5) { $str = str_replace(array('<font ', '</font>'), array('<span ', '</span>'), $str); $str = preg_replace('#color="(.*?)"#', 'style="color: \\1"', $str); } // Remove our artificially added PHP, and the syntax highlighting that came with it $str = preg_replace('/<span style="color: #([A-Z0-9]+)"><\?php( | )/i', '<span style="color: #$1">', $str); $str = preg_replace('/(<span style="color: #[A-Z0-9]+">.*?)\?><\/span>\n<\/span>\n<\/code>/is', "$1</span>\n</span>\n</code>", $str); $str = preg_replace('/<span style="color: #[A-Z0-9]+"\><\/span>/i', '', $str); // Replace our ers back to PHP tags. $str = str_replace(array('phptagopen', 'phptagclose', 'asptagopen', 'asptagclose', 'backslashtmp', 'scriptclose'), array('<?', '?>', '<%', '%>', '\\', '</script>'), $str); $str = str_replace( "#FF8000" ,"green" , $str); $str = str_replace( "#007700" ,"blue" , $str); return $str; } /** * Phrase Highlighter * * Highlights a phrase within a text string * * @access public * @param string the text string * @param string the phrase you'd like to highlight * @param string the openging tag to precede the phrase with * @param string the closing tag to end the phrase with * @return string */ public static function highlightPhrase($str, $phrase, $tag_open = '<strong>', $tag_close = '</strong>') { if ($str == '') { return ''; } if ($phrase != '') { return preg_replace('/('.preg_quote($phrase, '/').')/i', $tag_open."\\1".$tag_close, $str); } return $str; } public static function highlightSearch($keyword, $str) { $str = str_replace($keyword , "<font color='red'>$keyword</font>" , $str); return $str; } /** * 获取摘要简介信息 * * @param 原内容 $newContent * @param 取得长度 $length * @return String */ static function getBrief($newContent,$length = 100 , $end = '') { $pattern = "/<img[^>]*src\=('|\")(([^>]*)(jpg|gif|png|bmp|jpeg))\\1/i"; //获取所有图片标签的全部信 preg_match_all($pattern, $newContent, $matches); $str_out = ""; if( sizeof( $matches[2] )) { $str_out = "<img width = '30' src = '".$matches[2][0]."' onclick = 'resize_pic(this,30,176)'/>"; } $content = strip_tags($newContent); if(self::strlen_utf8($content)>$length) { $str_out .= self::subStringUtf8( $content,0,$length) . $end; } else { $str_out .= $content; } return $str_out; } Smarty 数组下标的问题 php生成excel linux定时执行url问题,在线等 [高手请进]php论坛&&帖子的时间存储问题 如何倒序读mysql数据库信息呢?语句如何写的呢>? php 中如何得到mssql存储过程得返回值?查了资料,没找到答案. excel和com的问题?望各位高手救救我 php配置问题,请教高手 请问哪位有文件下载的例子? 关于真静态化的疑问 求PHP高手,帮忙解决下集成问题 php 如何处理表单提交的数据才是安全的
* 得到utf8 的长度
* @author mu_rain
* @param String $str
* @return Int
*/
static function strlen_utf8($str) {
$i = 0;
$count = 0;
$len = strlen ($str);
while ($i < $len) {
$chr = ord ($str[$i]);
$count++;
$i++;
if($i >= $len) break;
if($chr & 0x80) {
$chr <<= 1;
while ($chr & 0x80) {
$i++;
$chr <<= 1;
}
}
}
return $count;
}
{
$st = preg_quote($startTag);
$et = preg_quote($endTag);
return mb_strlen(preg_replace("#{$st}[^{$et}]*{$et}#","~",$str),$encoding);
}
echo myLen("123asd你好[哈哈]");//9
[哈哈] == 1个字符
$str[0] 结果是九个字符
<?php
/**
* Created by JetBrains PhpStorm.
* User: Administrator
* Date: 12-2-17
* Time: 上午11:43
* To change this template use File | Settings | File Templates.
*/
/**
* 字符串处理基类.
* @author mu_rain
*/
class kString{
// ------------------------------------------------------------------------
/**
* replace the base64_encode to encode the string
*
* @param $str 对象的实例
* @package KDG
* @subpackage String
* @category Putils
* @author jim
* @return mixed
*/
// ------------------------------------------------------------------------
public static function encode($str){
$src = array("/","+","=");
$dist = array("-a","-b","-c");
$old = base64_encode($str);
$new = str_replace($src,$dist,$old);
return $new;
} // ------------------------------------------------------------------------
/**
* replace the base64_decode to decode the string
*
* @param $str 对象的实例
* @package P
* @subpackage String
* @category Putils
* @author jim
* @return mixed
*/
// ------------------------------------------------------------------------
public static function decode($str){
$src = array("-a","-b","-c");
$dist = array("/","+","=");
$old = str_replace($src,$dist,$str);
$new = base64_decode($old);
return $new;
} // ------------------------------------------------------------------------
/**
* replace the base64_decode to decode the string
*
* @param $str 对象的实例
* @package P
* @subpackage String
* @category Putils
* @author jim
* @return mixed
*/
// ------------------------------------------------------------------------
public static function showAsFileSize($str){ // change 1024 to 1k
$count = intVal($str);
$destSize = $count/1024;
$destSize = round($destSize,1);
return $destSize."K";
}
}
/**
* regulary show the string or object or json.
* 规格化显示
*
* @param $str 对象的实例
* @package P
* @subpackage String
* @category Putils
* @author jim
* @return mixed
*/
// ------------------------------------------------------------------------
public static function pr($array,$title = 'DEBUG',$type = 'array') {
$title .= date("Y-m-d H:i:s");
echo "<fieldset style=\"border: 1px solid rgb(0, 153, 0); margin: 20px 0pt; padding: 6px 10px 10px; background-color: rgb(238, 238, 238);\"><legend style=\"color: rgb(0, 153, 0);\">$title</legend>";
echo "<div style = 'font-size:14px; color:#000; border:1px solid #666; background:#ccc; padding:5px;'>";
print("<pre>");
if($type == 'json') {
$array = json_decode($array);
}
print_r($array);
print("</pre>");
echo "<div>";
echo "</fieldset>";
} // ------------------------------------------------------------------------
/**
*
* 去掉html标记。
* strip the html
* @param unknown_type $string
*/
// ------------------------------------------------------------------------
public static function stripHtml($string){
$search = array ("'<script[^>]*?>.*?</script>'si", // 去掉 javascript 4B+1ZsmMd
"'<[/!]*?[^<>]*?>'si", // 去掉 HTML 标记
"'([rn])[s]+'", // 去掉空白字符
"'&(quot|#34);'i", // 替换 HTML 实体
"'&(amp|#38);'i",
"'&(lt|#60);'i",
"'&(gt|#62);'i",
"'&(nbsp|#160);'i",
"'&(iexcl|#161);'i",
"'&(cent|#162);'i",
"'&(pound|#163);'i",
"'&(copy|#169);'i",
"'&#(d+);'e");
$replace = array(
"",
"",
"\1",
"\"",
"&",
"<",
">",
" ",
chr(161),
chr(162),
chr(163),
chr(169),
"chr(\1)");
return preg_replace($search, $replace, $string);
} // ------------------------------------------------------------------------
/**
*
* 求子字符串
* @param unknown_type $str
* @param unknown_type $start
* @param unknown_type $lenth
*/
// -------------------------------------------------------------------------+
public static function subStringUtf8($str, $start, $lenth)
{
$len = strlen($str);
$r = array ();
$n = 0;
$m = 0;
for ($i = 0; $i < $len; $i++)
{
$x = substr($str, $i, 1);
$a = base_convert(ord($x), 10, 2);
$a = substr('00000000' . $a, -8);
if ($n < $start)
{
if (substr($a, 0, 1) == 0)
{
}
elseif (substr($a, 0, 3) == 110)
{
$i += 1;
}
elseif (substr($a, 0, 4) == 1110)
{
$i += 2;
}
$n++;
}
else
{
if (substr($a, 0, 1) == 0)
{
$r[] = substr($str, $i, 1);
}
elseif (substr($a, 0, 3) == 110)
{
$r[] = substr($str, $i, 2);
$i += 1;
}
elseif (substr($a, 0, 4) == 1110)
{
$r[] = substr($str, $i, 3);
$i += 2;
}
else
{
$r[] = '';
}
if (++ $m >= $lenth)
{
break;
}
}
}
return join("", $r);
} public static function readOnly($val)
{
if($val) return "readonly = 'readonly' style = 'color:gray; border:1px solid gray;'";
} public static function highlightCode($str)
{
// The highlight string function encodes and highlights
// brackets so we need them to start raw
$str = str_replace(array('<', '>'), array('<', '>'), $str); // Replace any existing PHP tags to temporary ers so they don't accidentally
// break the string out of PHP, and thus, thwart the highlighting. $str = str_replace(array('<?', '?>', '<%', '%>', '\\', '</script>'),
array('phptagopen', 'phptagclose', 'asptagopen', 'asptagclose', 'backslashtmp', 'scriptclose'), $str); // The highlight_string function requires that the text be surrounded
// by PHP tags, which we will remove later
$str = '<?php '.$str.' ?>'; // <? // All the magic happens here, baby!
$str = highlight_string($str, TRUE); // Prior to PHP 5, the highligh function used icky <font> tags
// so we'll replace them with <span> tags. if (abs(PHP_VERSION) < 5)
{
$str = str_replace(array('<font ', '</font>'), array('<span ', '</span>'), $str);
$str = preg_replace('#color="(.*?)"#', 'style="color: \\1"', $str);
} // Remove our artificially added PHP, and the syntax highlighting that came with it
$str = preg_replace('/<span style="color: #([A-Z0-9]+)"><\?php( | )/i', '<span style="color: #$1">', $str);
$str = preg_replace('/(<span style="color: #[A-Z0-9]+">.*?)\?><\/span>\n<\/span>\n<\/code>/is', "$1</span>\n</span>\n</code>", $str);
$str = preg_replace('/<span style="color: #[A-Z0-9]+"\><\/span>/i', '', $str); // Replace our ers back to PHP tags.
$str = str_replace(array('phptagopen', 'phptagclose', 'asptagopen', 'asptagclose', 'backslashtmp', 'scriptclose'),
array('<?', '?>', '<%', '%>', '\\', '</script>'), $str);
$str = str_replace( "#FF8000" ,"green" , $str);
$str = str_replace( "#007700" ,"blue" , $str); return $str;
} /**
* Phrase Highlighter
*
* Highlights a phrase within a text string
*
* @access public
* @param string the text string
* @param string the phrase you'd like to highlight
* @param string the openging tag to precede the phrase with
* @param string the closing tag to end the phrase with
* @return string
*/
public static function highlightPhrase($str, $phrase, $tag_open = '<strong>', $tag_close = '</strong>')
{
if ($str == '')
{
return '';
}
if ($phrase != '')
{
return preg_replace('/('.preg_quote($phrase, '/').')/i', $tag_open."\\1".$tag_close, $str);
} return $str;
} public static function highlightSearch($keyword, $str)
{
$str = str_replace($keyword , "<font color='red'>$keyword</font>" , $str);
return $str;
} /**
* 获取摘要简介信息
*
* @param 原内容 $newContent
* @param 取得长度 $length
* @return String
*/
static function getBrief($newContent,$length = 100 , $end = '')
{
$pattern = "/<img[^>]*src\=('|\")(([^>]*)(jpg|gif|png|bmp|jpeg))\\1/i"; //获取所有图片标签的全部信
preg_match_all($pattern, $newContent, $matches); $str_out = "";
if( sizeof( $matches[2] ))
{
$str_out = "<img width = '30' src = '".$matches[2][0]."' onclick = 'resize_pic(this,30,176)'/>";
} $content = strip_tags($newContent); if(self::strlen_utf8($content)>$length)
{
$str_out .= self::subStringUtf8( $content,0,$length) . $end;
}
else
{
$str_out .= $content;
}
return $str_out;
}