• 欢迎访问搞代码网站,推荐使用最新版火狐浏览器和Chrome浏览器访问本网站!
  • 如果您觉得本站非常有看点,那么赶紧使用Ctrl+D 收藏搞代码吧

php 针对中英文混合排版之字符串常用的函数

php 搞代码 3年前 (2022-01-22) 17次浏览 已收录 0个评论

# 判断某个位置是中文字符的左还是右半部分,或不是中文
# 返回值 -1 左 0 不是中文字符 1 右
# 用法

/*  $a = 'this is 中文';  print is_chinese($a, 1); // 0  print is_chinese($a,8); // -1  print is_chinese($a,9); // 1  */  function is_chinese(&$str, $location) {  $ch = true;  $i = $location;  while(ord($str[$i])>0xa0 && $i >= 0) {  $ch = !$ch;  $i --;  }  if($i != $location) {  $f_str = $ch ? 1: -1;  }  else {  $f_str = false;  }  return $f_str;  }  # 中文字符串倒置函数  # 如果一个将一个有中文的字符串用strrev倒过来,就会产生乱码  /*  print cstrrev('this is 中文'); // 文中 si siht  */  function cstrrev(&$str) {  $long = strlen($str);  for($f_str='', $chinese=false, $i=$long-1; $i>=0; $i--) {  if(ord($str[$i]) > 0xa0) {  $chinese = ! $chinese;  if($chinese == false) {  $f_str .= $str[$i].$str[$i+1];  }  }  else {  $f_str .= $str[$i];  }  }  return $f_str;  }  /* 中文字符串截取函数  一些中文字符串截取函数经常有一些问题,例如在一些自动换行程序中  $a=“1中2”;  经两次截取后,  csubstr($str,$a,0,2);  csubstr($str, $a, 2,2)  由于载取位置指向“中”的右字节,可能会是这样的结果  1, 2  用本函数会产生正确的结果  1中, 2  */  # start 开始位置,从0开始  # long = 0 则从start 一直取到字符串尾  # ltor = true 时从左到右取字符,false 时到右到左取字符  # $cn_len 中文字符按字节取还是字数取,如果按字数取,则一个中文当一个字节计算  function csubstr(&$str, $start=0, $long=0, $ltor=true, $cn_len=2) {  if($long == 0) $long = strlen($str);  if($ltor == false) $str = cstrrev($str);  if($cn_len == 1) {  for($i=0, $fs=0; $i<$start; $fs++)  $i += (ord($str[$fs]) <= 0xa0) ? 1 : 0.5;  for($i=0, $fe=$fs; $i<$long; $fe++)  $i += (ord($str[$fe]) <= 0xa0) ? 1 : 0.5;  $long = $fe - $fs;  }  else {  $fs = (is_chinese($str, $start) == 1) ? $start - 1 : $start;  $fe = $long + $start - 1;  $end = ( is_chinese($str, $fe) == -1 ) ? $fe -1 : $fe;  $long = $end - $fs + 1;  }  $f_str = substr($str, $fs, $long);  if($ltor == false) $f_str = cstrrev($f_str);  return $f_str;  }  # 取左字符串  # 当cn_len == 2 时 $long 取左边多少个字,反之则取左边多少个字节  functi<div>)本文来源gaodai.ma#com搞#代!码网_</div><strong>搞代gaodaima码</strong>on cleft(&$str, $long, $cn_len=2) {  $f_str = csubstr($str, 0, $long, true, $cn_len);  return $f_str;  }  # 取右字符串  function cright(&$str, $long, $cn_len=2) {  $f_str = cstrrev($str);  $f_str = csubstr($f_str, 0, $long, true, $cn_len);  $f_str = cstrrev($f_str);  return $f_str;  }  # 对含有中文字符的文章分行格式化  # 再也不会发生因换行问题而产生的种种问题啦!!!  # 注:文章的每一行必须用 n (chr(13))进行分行  # $width 每行多少字符  # $br 将 每行用什么字符当结束符  function ctext_wrap(&$text, $width=60, $br="<BR>") {  $lines = explode("n",$text);  $rows = count($lines);  for($i=0; $i<$rows; $i++) {  $len = strlen($lines[$i]);  for($j=0; $j<$len; $j+=$width) {  $p = $j + $width - 1;  $k = 0;  if($p<$len) {  while(!is_chinese($lines[$i], $p) && $lines[$i][$p] != ' ' && $p>$j) {  $k ++;  $p --;  }  if($p == $j) $k = 0;  }  $f_str .= csubstr($lines[$i], $j, $width-$k) . $br;  $j -= $k;  }  }  return $f_str;  }

以上就是php 针对中英文混合排版之字符串常用的函数的详细内容,更多请关注搞代码gaodaima其它相关文章!


搞代码网(gaodaima.com)提供的所有资源部分来自互联网,如果有侵犯您的版权或其他权益,请说明详细缘由并提供版权或权益证明然后发送到邮箱[email protected],我们会在看到邮件的第一时间内为您处理,或直接联系QQ:872152909。本网站采用BY-NC-SA协议进行授权
转载请注明原文链接:php 针对中英文混合排版之字符串常用的函数
喜欢 (0)
[搞代码]
分享 (0)
发表我的评论
取消评论

表情 贴图 加粗 删除线 居中 斜体 签到

Hi,您需要填写昵称和邮箱!

  • 昵称 (必填)
  • 邮箱 (必填)
  • 网址