php中文字元串
『壹』 php 將字元串中 中文替換
$str
=
"我是chinese,龍的傳復人!";
//匹配任意制中文字元的表達式
$pattern
=
"/[\x7f-\xff]/sim";
$replacement
=
'';
//用該表達式替換,將替換掉字元串中出現的任意中文字元,包括中文符號。
echo
'result:'.preg_replace($pattern,
$replacement,
$str);
//結果為:chinese,!
『貳』 php匹配中文字元串
如果確定城市名左右絕對有「省」「市」的話,可以這樣:
$str="XXXXXXXXXXX省杭州市XXXXXXXXXXXXXXX市場經濟XXXXX";
preg_match('/省([\x80-\xff]+)市/',$str,$result);
print_r($r);
『叄』 PHP 查找中文字元串
在源碼的字元編碼和頁面的字元編碼一致時,
strpos查找不會出錯。
但非常值得注意的是strpos的返回值
當找到時返回的是數字位置,沒有找到返回FALSE
但是首個字元位置找到返回的是0
當疏忽時,如果用
$p= strpos(...);
if($p==false) 這時條件是成立的,造成誤判
安全地使用strpos必用===類型全等符號,改成。
if($p===false)
這樣一般就不會出錯。
『肆』 PHP如何獲取中文字元串長度 utf8
PHP對中文字元串的處理一直困擾於剛剛接觸PHP開發的新手程序員。下面簡要的剖析一下PHP對中文字元串長度的處理:
PHP自帶的函數如strlen()、mb_strlen()都是通過計算字元串所佔位元組數來統計字元串長度的,一個英文字元佔1位元組。例:
$enStr = 『Hello,China!』;
echo strlen($enStr); // 輸出:12
而中文則不然,做中文網站一般會選擇兩種編碼:gbk/gb2312或是utf-8。utf-8能兼容更多的字元,所以受到很多站長的喜愛。gbk與utf-8對中文的編碼不同,導致中文在gbk與utf-8編碼下所佔位元組也有差異。
gbk編碼下每個中文字元所佔位元組為2,例:
$zhStr = 『您好,中國!』;
echo strlen($zhStr); // 輸出:12
utf-8編碼下每個中文字元所佔位元組為3,例:
$zhStr = 『您好,中國!』;
echo strlen($zhStr); // 輸出:18
那麼如何計算這組中文字元串的長度呢?有人可能會說gbk下獲取中文字元串長度除以2,utf-8編碼下除以3不就行了嗎?但是您要考慮字元串並不老實,99%的情況會以中英混合的情況出現。
這是WordPress中的一段代碼,主要思想就是先用正則將字元串分解為個體單元,然後再計算單元的個數即字元串的長度,代碼如下(只能處理utf-8編碼下的字元串):
$zhStr = 『您好,中國!』;
$str = 『Hello,中國!』;
// 計算中文字元串長度
function utf8_strlen($string = null) {
// 將字元串分解為單元
preg_match_all(「/./us」, $string, $match);
// 返回單元個數
return count($match[0]);
}
echo utf8_strlen($zhStr); // 輸出:6
echo utf8_strlen($str); // 輸出:9
『伍』 php 替換中文字元串函數
preg_replace('/\@小明/','回復我',$a);
『陸』 php 怎麼截取漢字字元串
^<?php
$str='這樣一來我的字元串就不會有亂碼^_^';
echo"mb_substr:".mb_substr($str,0,7,'utf-8');
//結果:這樣一來我的字
echo"<br>";
echo"mb_strcut:".mb_strcut($str,0,6,'utf-8');
//結果:這樣
?>
首先
1.確保你的Windows/system32下有php_mbstring.dll這個文件,沒有就從你Php安裝目錄extensions里拷入Windows/system32裡面。
2.在windows目錄下找到php.ini打開編輯,搜索mbstring.dll,找到
;extension=php_mbstring.dll把前面的;號去掉,這樣mb_substr函數就可以生效了
mb_strcut函數功能也可以截取字元串長度,下面實例具體看看區別在哪:
mb_substr();
『柒』 php中截取中文字元串方法
mb_substr — 獲取部分字元串
說明
mb_substr ( string $str , int $start [, int $length = NULL [, string $encoding = mb_internal_encoding() ]] ) : string
根據字元數執行一個多位元組安全的 substr() 操作。 位置是從 str 的開始位置進行計數。 第一個字元的位置是 0。第二個字元的位置是 1,以此類推。
參數
str
從該 string 中提取子字元串。
start
如果 start 不是負數,返回的字元串會從 str 第 start 的位置開始,從 0 開始計數。舉個例子,字元串 'abcdef',位置 0 的字元是 'a',位置 2 的字元是 'c',以此類推。
如果 start 是負數,返回的字元串是從 str 末尾處第 start 個字元開始的。
length
str 中要使用的最大字元數。如果省略了此參數或者傳入了 NULL,則會提取到字元串的尾部。
encoding
encoding 參數為字元編碼。如果省略,則使用內部字元編碼。
『捌』 如何去掉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);
}
『玖』 php 如何比較兩個中文字元串是否相等
普通比較用==類型和字元都比較用=== 即全等 比如 1和true用===比較的回話就是不相等答,用==比較的話就是相等的(前者是int型後者是bool型)1和"1"用===比較的話就是不相等,用==比較的話就相等(前者是int型,後者是str型)另:abc和Abc用==比較是不相等的.如果你在比較時出現另一個比較對像有可能大寫時,可以用strtolower把那個可能出現大寫的字元串轉成全小寫的再比較.