導航:首頁 > 編程語言 > php字元串替換中文

php字元串替換中文

發布時間:2022-09-10 11:30:16

『壹』 php如何替換中文字元串最後一個字,我要把句子中的 "啊、呀、哈"這些沒用的詞都換成"啊" ,怎麼實現最快

你問的是兩個問題:

1、替換中文字元串的最後一個字;(最後?)
2、替換中文字元串中的「啊,哈,呀...」為「啊」;

這兩個問題不能同時一次解決,比如字元串是「哈哈」結尾,替換最後一個字變成:哈啊?

我覺得你就是要解決問題2:

$pattern = array('啊','呀','哈');
$string = str_replace($pattern, '啊', string);

能看明白么?樓上的方法也可以,不過有些效率低,維護不方便;
$pattern以後可以從資料庫里讀,你就能更好弄了~~

至於問題1,建議再考慮考慮~

補充,關於中文字元串的編碼問題,我沒寫,就是iconv的問題,自己來吧!

『貳』 PHP替換字元串中的繁體字元串的問題【絕對挑戰,2011年最難的PHP替換繁體中文問題】

用String 中的方法替換

『叄』 php 將字元串中 中文替換

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

『肆』 如何用PHP把下面字元串中的漢字規製成其它符號。

$string=mb_ereg_replace("/日期:s/","-",$string);
$string=mb_ereg_replace("/艙位等級:s/","-",$string);

『伍』 php中的替換中文字元串問題

還有一個函數叫ereg_replace()
使用正則表達式進行替換的。可以試一下。

『陸』 php裡面字元串函數str_replace(); 什麼意思

str_replace():是替換字元串

『柒』 如何去掉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字元串對應替換(把一些拼音換成對應的漢字)

<?php
$city
=
'anyang';
$citys
=
array('shanghai'=>'上海',『anyang』=>'安陽'.....);
$city
=
$citys[$city];
做個一一對應的hash,然後替換咯

『玖』 php替換一段中文字元串里的幾個字的正則怎麼寫

在寫小偷吧,我上個月參考了其它源碼,也寫了一個。你說的這個問題我也遇到了,str_replace這個函數可以替換中文,因為你的編碼不統一所以才出現這個問題。把抓了來的代碼轉成utf8,你的網頁編碼也得試utf8碼,charset最好也設成utf8

閱讀全文

與php字元串替換中文相關的資料

熱點內容
美食博主用什麼app拍視頻 瀏覽:812
ipone手機如何加密微信 瀏覽:354
自來水加密閥閥帽 瀏覽:431
華為交換機dhcp配置命令 瀏覽:315
androidbitmap縮小 瀏覽:271
單片機串口控制燈 瀏覽:84
大訊雲伺服器安裝視頻 瀏覽:784
華為演算法領先世界 瀏覽:654
linux路由重啟 瀏覽:566
php的模板編程 瀏覽:320
編譯器原理與實現書 瀏覽:709
dos選擇命令 瀏覽:17
apm固件編譯到單片機 瀏覽:121
聯通深藍卡都包含什麼app 瀏覽:264
如何判斷網路伺服器正常 瀏覽:652
路由器搭橋遠端伺服器地址是什麼 瀏覽:518
編譯動態庫時會連接依賴庫嗎 瀏覽:710
淘寶手機加密是隨機的嗎 瀏覽:675
解壓包子怎麼裝飾 瀏覽:588
四個數湊24演算法 瀏覽:679