php中文标点
1. php "str_split"函数分割中文字符串乱码问题……
一个解决方案.
测试发现,preg_split对中文(多字节)的分隔符处理可能真的存在问题.
原因可能是正则匹配时不能正常分隔一个多字节字符(猜测).
但我实验用半角(英文)的分隔符就工作得很好.
于是
我在处理待分隔的文本前先对文本进行替换,把中文的句点和逗号都替换成英文半角的.然后再使用preg_split,暂时发现工作得不错.
以下是我的测试代码.
<?php
$test = <<<EOF
记者从有关人士处获悉,嫦娥二号发射的各项准备工作全部就绪,昨日经专家组评审,卫星、火箭、发射场、测控等系统均情况正常,具备发射条件。今天起,西昌卫星发射中心的加注手将为火箭加注燃料。
据一位航天专家介绍,因为地球和月球都在转动的缘故,所以地月之间的最佳交汇点每年仅出现3次,而这3次便是发射探月卫星的最佳节点,经过观测,今年3次的出现时间分别为10月1日、10月2日和10月3日,而最佳发射窗口则是1日晚7时,2日晚8时和3日晚10时,其中国庆日晚7时为佳中之佳。
针对媒体称10月1日发射窗口时间为晚6时59分57秒,该专家告诉本报记者,早3秒并非提前3秒发射,而是将这3秒时间预留出来,作为现场指挥员在发送倒计时口令的缓冲时间。(记者万强)
EOF;
//$input = $_POST[$content];    //........................获取要分割的字符串
$test = str_replace(",",',',$test);
$test = str_replace("。",'.',$test);
$mode="/[,|.]/s";       //......................用“逗号” 和 “句号” 分割字符串 
$output = preg_split($mode,$test,-1);
print_r($output);
?>
============
楼主不防试试,我的代码是gb2312的字串.
经测试在,字符串为UTF8时,也可以正常工作.
2. 如何去掉PHP字符串中的中文字符
我给你个PHP截取中文字符串的方法总结,有原理,有源码:程序一:PHP截取中文字符串方法由于网站首页以及vTigerCRM里经常在截取中文字符串时出现乱码(使用substr),今天找到一个比较好的截取中文字符串方法,在此与大家共享。function msubstr($str, $start, $len) {
    $tmpstr = "";
    $strlen = $start + $len;
    for($i = 0; $i < $strlen; $i++) {
        if(ord(substr($str, $i, 1)) > 0xa0) {
            $tmpstr .= substr($str, $i, 2);
            $i++;
        } else
            $tmpstr .= substr($str, $i, 1);
    }
    return $tmpstr;
}程序二:PHP截取UTF-8字符串,解决半字符问题/******************************************************************
* PHP截取UTF-8字符串,解决半字符问题。
* 英文、数字(半角)为1字节(8位),中文(全角)为3字节
* @return 取出的字符串, 当$len小于等于0时, 会返回整个字符串
* @param $str 源字符串
* $len 左边的子串的长度
****************************************************************/
function utf_substr($str,$len)
{
for($i=0;$i<$len;$i++)
{
$temp_str=substr($str,0,1);
if(ord($temp_str) > 127)
{
$i++;
if($i<$len)
{
$new_str[]=substr($str,0,3);
$str=substr($str,3);
}
}
else
{
$new_str[]=substr($str,0,1);
$str=substr($str,1);
}
}
return join($new_str);
}
?>php utf-8 字符串截取<?
function cutstr($string, $length) {
        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]/", $string, $info); 
        for($i=0; $i<count($info[0]); $i++) {
                $wordscut .= $info[0][$i];
                $j = ord($info[0][$i]) > 127 ? $j + 2 : $j + 1;
                if ($j > $length - 3) {
                        return $wordscut." ...";
                }
        }
        return join('', $info[0]);
}
$string="242432反对感是456犯得上广泛大使馆地方7890";
for($i=0;$i<strlen($string);$i++)
{
echo cutstr($string,$i)."<br>";
}
?>
截取utf-8字符串函数为了支持多语言,数据库里的字符串可能保存为UTF-8编码,在网站开发中可能需要用php截取字符串的一部分。为了避免出现乱码现象,编写如下的UTF-8字符串截取函数关于utf-8的原理请看 UTF-8 FAQUTF-8编码的字符可能由1~3个字节组成, 具体数目可以由第一个字节判断出来。(理论上可能更长,但这里假设不超过3个字节)
第一个字节大于224的,它与它之后的2个字节一起组成一个UTF-8字符
第一个字节大于192小于224的,它与它之后的1个字节组成一个UTF-8字符
否则第一个字节本身就是一个英文字符(包括数字和一小部分标点符号)。以前为某网站设计的代码(也是现在用在首页的长度截取的函数)
Code:
<?php // Cut_Str;
//$sourcestr 是要处理的字符串 
//$cutlength 为截取的长度(即字数) 
function cut_str($sourcestr,$cutlength) 
{ 
   $returnstr=''; 
   $i=0; 
   $n=0; 
   $str_length=strlen($sourcestr);//字符串的字节数 
   while (($n<$cutlength) and ($i<=$str_length)) 
   { 
      $temp_str=substr($sourcestr,$i,1); 
      $ascnum=Ord($temp_str);//得到字符串中第$i位字符的ascii码 
      if ($ascnum>=224)    //如果ASCII位高与224,
      { 
         $returnstr=$returnstr.substr($sourcestr,$i,3); //根据UTF-8编码规范,将3个连续的字符计为单个字符         
         $i=$i+3;            //实际Byte计为3
         $n++;            //字串长度计1
      }
      elseif ($ascnum>=192) //如果ASCII位高与192,
      { 
         $returnstr=$returnstr.substr($sourcestr,$i,2); //根据UTF-8编码规范,将2个连续的字符计为单个字符 
         $i=$i+2;            //实际Byte计为2
         $n++;            //字串长度计1
      }
      elseif ($ascnum>=65 && $ascnum<=90) //如果是大写字母,
      { 
         $returnstr=$returnstr.substr($sourcestr,$i,1); 
         $i=$i+1;            //实际的Byte数仍计1个
         $n++;            //但考虑整体美观,大写字母计成一个高位字符
      }
      else                //其他情况下,包括小写字母和半角标点符号,
      { 
         $returnstr=$returnstr.substr($sourcestr,$i,1); 
         $i=$i+1;            //实际的Byte数计1个
         $n=$n+0.5;        //小写字母和半角标点等与半个高位字符宽...
      } 
   } 
         if ($str_length>$cutlength){
          $returnstr = $returnstr . "...";//超过长度时在尾处加上省略号
      }
    return $returnstr;}截取utf-8字符串函数function FSubstr($title,$start,$len="",$magic=true) 
{
/**
* powered by Smartpig
* mailto:[email protected]
*/if($len == "") $len=strlen($title);
if($start != 0)
{
$startv = ord(substr($title,$start,1));
if($startv >= 128)
{
   if($startv < 192)
   {
    for($i=$start-1;$i>0;$i--)
    {
     $tempv = ord(substr($title,$i,1));
     if($tempv >= 192) break;
    }
    $start = $i;
   }
}
}
if(strlen($title)<=$len) return substr($title,$start,$len);
$alen   = 0;
$blen = 0;
$realnum = 0;
for($i=$start;$i<strlen($title);$i++)
{
$ctype = 0;
$cstep = 0;
$cur = substr($title,$i,1);
if($cur == "&")
{
   if(substr($title,$i,4) == "<")
   {
    $cstep = 4;
    $length += 4;
    $i += 3;
    $realnum ++;
    if($magic)
    {
     $alen ++;
    }
   }
   else if(substr($title,$i,4) == ">")
   {
    $cstep = 4;
    $length += 4;
    $i += 3;
    $realnum ++;
    if($magic)
    {
     $alen ++;
    }
   }
   else if(substr($title,$i,5) == "&")
   {
    $cstep = 5;
    $length += 5;
    $i += 4;
    $realnum ++;
    if($magic)
    {
     $alen ++;
    }
   }
   else if(substr($title,$i,6) == """)
   {
    $cstep = 6;
    $length += 6;
    $i += 5;
    $realnum ++;
    if($magic)
    {
     $alen ++;
    }
   }
   else if(preg_match("/(d+);?/i",substr($title,$i,8),$match))
   {
    $cstep = strlen($match[0]);
    $length += strlen($match[0]);
    $i += strlen($match[0])-1;
    $realnum ++;
    if($magic)
    {
     $blen ++;
     $ctype = 1;
    }
   }
}else{
   if(ord($cur)>=252)
   {
    $cstep = 6;
    $length += 6;
    $i += 5;
    $realnum ++;
    if($magic)
    {
     $blen ++;
     $ctype = 1;
    }
   }elseif(ord($cur)>=248){
    $cstep = 5;
    $length += 5;
    $i += 4;
    $realnum ++;
    if($magic)
    {
     $ctype = 1;
     $blen ++;
    }
   }elseif(ord($cur)>=240){
    $cstep = 4;
    $length += 4;
    $i += 3;
    $realnum ++;
    if($magic)
    {
     $blen ++;
     $ctype = 1;
    }
   }elseif(ord($cur)>=224){
    $cstep = 3;
    $length += 3;
    $i += 2;
    $realnum ++;
    if($magic)
    {
     $ctype = 1;
     $blen ++;
    }
   }elseif(ord($cur)>=192){
    $cstep = 2;
    $length += 2;
    $i += 1;
    $realnum ++;
    if($magic)
    {
     $blen ++;
     $ctype = 1;
    }
   }elseif(ord($cur)>=128){
    $length += 1;
   }else{
    $cstep = 1;
    $length +=1;
    $realnum ++;
    if($magic)
    {
     if(ord($cur) >= 65 && ord($cur) <= 90)
     {
      $blen++;
     }else{
      $alen++;
     }
    }
   }
}
if($magic)
{
   if(($blen*2+$alen) == ($len*2)) break;
   if(($blen*2+$alen) == ($len*2+1))
   {
    if($ctype == 1)
    {
     $length -= $cstep;
     break;
    }else{
     break;
    }
   }
}else{
   if($realnum == $len) break;
}
}
unset($cur);
unset($alen);
unset($blen);
unset($realnum);
unset($ctype);
unset($cstep);
return substr($title,$start,$length);
}
3. php 求用正则匹配中文以及标点符号
你指的连续,那么就要求是两个以上了:
1
   
$reg ='/[\x80-\xff]{2,}/';
这个正则是表示匹配两个字节以上的多字节字符(多字节字符通常高位为 1)。
4. php怎么把匹配到的一段字符去掉标点符号再替换进去
用php的字符串替换函数就可以了,str_replace(),把逗号替换成空字符串,希望对你有帮助

