當前位置:首頁 » 編程語言 » php字元串包含中文

php字元串包含中文

發布時間: 2022-05-07 18:04:54

A. 如何去掉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);
}

B. php 將字元串中 中文替換

$str
=
"我是chinese,龍的傳人!";
//匹配任意中文字元的表達式
$pattern
=
"/[\x7f-\xff]/sim";
$replacement
=
'';
//用該表達式替換,將替換掉字元串中出現的任意中文字元,包括中文符號。
echo
'result:'.preg_replace($pattern,
$replacement,
$str);
//結果為:chinese,!

C. php代碼 字元串有中文也有數字,如何取數字

使用字元串拆分成數組。然後在判斷數組元素是否為數值型。

字元串涉及字元編碼。比較麻煩。簡單點的。替換date 值。

將 「上午」「點」「下午」「晚上」「早上」「中午」等能出現的字元。全部替換成空

D. php正則判斷字元串是否含有中文

functionchk_cn($str){
if(preg_match('|[x{4e00}-x{9fa5}]+?|u',$str)){
returntrue;
}
returnfalse;
}

調用這個 函數,返回true則是含有中文,false則無中文。

另外,x{4e00}-x{9fa5} 這個范圍我也不太確定是不是包含所有的中文范圍,沒有認真研究它的取值范圍。

E. php中判斷字元串是否全是中文或含有中文的實現代碼

可以,直接封裝一個函數給你:

<?php

functioncheckChinese($string)
{
if(preg_match('/^[x{4e00}-x{9fa5}]+$/u',$string)===1){
//全是中文
return1;
}elseif(preg_match('/[x{4e00}-x{9fa5}]/u',$string)===1){
//包含中文
return0;
}
return-1;
}

$string="手機1141";

echocheckChinese($string);

當全部是中文的時候,函數返回 1;當有一部分是中文的時候,函數返回 0; 當不包含中文的時候,函數返回 -1。

F. 如何用PHP查找字元串是否有某中文字元

如果是判斷字元串是否包含中文可以用正則/[u4e00-u9fa5]/

如果只是查找是否有某中文字元用strpos就行了

比如查找字元串是否有「中」這個中文

if(strpos('查找字元串是否有某中文字元','中')!==false){
echo'包含"中"';
}else{
echo'不含"中"';
}

G. php中判斷字元串是否全是中文或含有中文的

123456789101112<?$str = "測試中文";echo $str;echo "<hr>";//if (preg_match("/^[".chr(0xa1)."-".chr(0xff)."]+$/", $str)) { //只能在GB2312情況下使用//if (preg_match("/^[\x7f-\xff]+$/", $str)) { //兼容gb2312,utf-8 //判斷字元串是否全是中文if (preg_match("/[\x7f-\xff]/", $str)) { //判斷字元串中是否有中文echo "正確輸入";} else {echo "錯誤輸入";}?>

H. PHP判斷字元串中是否含有中文

一,判斷全是中文
$str="'324是";
if(!eregi("[^\x80-\xff]","$str")){
echo "全是中文";
}else{
echo "不是";
}
二,判斷含有中文
$str = "中文";
if (preg_match("/[\x7f-\xff]/", $str)) {
echo "含有中文";
}else{
echo "沒有中文";
}

$pattern = '/[^\x00-\x80]/';
if(preg_match($pattern,$str)){
echo "含有中文";
}else{
echo "沒有中文";
}
如果還有問題可以去後盾網論壇問題求助專區,希望我的回答對你有幫助

I. PHP 查找中文字元串

在PHP中查找中文字元,有兩種方案:

  1. 將PHP保存為與欲查找中文一樣的編碼,然後使用strpos查找。

  2. 將PHP保存為UTF-8無BOM編碼,然後轉換字元串編碼為UTF-8,再用strpos查找。


第一種方法:

strpos($curl_res,'中文');

第二種方法:

$str=mb_convert_encoding($str,'utf-8','gbk');
mb_strpos($str,'中文');


主意:函數中的中文字元串參數的編碼,必須要和PHP文件保存格式的編碼一致。

J. php 字元串判斷是否含有某個字 中文

$str1 = 'asdfFSDdda';
$str2 = 'Fs';
//strpos 大小寫敏感 stripos大小寫不敏感 兩個函數都是返回str2 在str1 第一次出現的位置
if(strpos($str1,$str2) === false){ //使用絕對等於
//不包含
}else{
//包含
}詳細內容可咨詢遠標老師

熱點內容
ct5推薦哪個配置 發布:2025-05-11 11:47:45 瀏覽:737
領購未上傳發票 發布:2025-05-11 11:43:27 瀏覽:716
查看華為雲伺服器的ip地址 發布:2025-05-11 11:24:44 瀏覽:235
長沙銀行密碼多少 發布:2025-05-11 11:24:38 瀏覽:671
緩存手機視頻合並軟體哪個好 發布:2025-05-11 11:22:30 瀏覽:698
伺服器c盤怎麼清除 發布:2025-05-11 11:16:33 瀏覽:39
動態估演算法 發布:2025-05-11 11:06:19 瀏覽:923
sql2008使用教程 發布:2025-05-11 10:53:16 瀏覽:315
正態分布函數c語言 發布:2025-05-11 10:50:38 瀏覽:396
分類投票源碼 發布:2025-05-11 10:46:23 瀏覽:782