㈠ 如何去掉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如何截取漢字長度啊
function cutstr($string, $length, $dot = '') {
global $dbcharset;
$length*=2;
if(strlen($string) <= $length) {
return $string;
}
$string = str_replace(array('&', '"', '<', '>'), array('&', '"', '<', '>'), $string);
$strcut = '';
if(strtolower($dbcharset) == 'utf8') {
$n = $tn = $noc = 0;
while($n < strlen($string)) {
$t = ord($string[$n]);
if($t == 9 || $t == 10 || (32 <= $t && $t <= 126)) {
$tn = 1; $n++; $noc++;
} elseif(194 <= $t && $t <= 223) {
$tn = 2; $n += 2; $noc += 2;
} elseif(224 <= $t && $t < 239) {
$tn = 3; $n += 3; $noc += 2;
} elseif(240 <= $t && $t <= 247) {
$tn = 4; $n += 4; $noc += 2;
} elseif(248 <= $t && $t <= 251) {
$tn = 5; $n += 5; $noc += 2;
} elseif($t == 252 || $t == 253) {
$tn = 6; $n += 6; $noc += 2;
} else {
$n++;
}
if($noc >= $length) {
break;
}
}
if($noc > $length) {
$n -= $tn;
}
$strcut = substr($string, 0, $n);
} else {
for($i = 0; $i < $length; $i++) {
$strcut .= ord($string[$i]) > 127 ? $string[$i].$string[++$i] : $string[$i];
}
}
$strcut = str_replace(array('&', '"', '<', '>'), array('&', '"', '<', '>'), $strcut);
return $strcut.$dot;
}
㈢ PHP字元串轉換問題!謝謝回答。
還有個情況。你的php是不是用utf-8?輸出所在的html裡面,代碼明確告訴瀏覽器這裡面用的是不是用這個字元集?
㈣ 如何用php做出登陸注冊留言板
用php做出登陸注冊留言板:
<form id="form1" name="form1" method="post" action="<?php echo site_url()."/publish/user_message"?>">
<textarea rows="5" cols="50" name="huifu" <?php if($uere_name == "0"){echo "disabled";}?> >
<?php
if($uere_name == "0")
{echo "抱歉你還沒登錄不能進行留言";}
?>
</textarea>
<input class="wole" name="author" value="<?php echo $author;?>" /><!--接受方帖子作者-->
<input class="wole" name="news_id" value="<?php echo $news_idx;?>" /><!--文章id-->
<input type="submit" name="Submit"/>
</form>
<script language="javascript">
function updateinfo(){
if(<?php echo $uere_name;?> == 1){
document.form1.Submit.value = "留言";
document.form1.Submit.disabled = false;
}
else{
document.form1.Submit.value = "還未登錄";
document.form1.Submit.disabled = "disabled";
}
}
updateinfo();
</script>
回復帖子:
<p>這里是<?php echo $is;?>樓 用戶:<?php echo $sel->receiver_author;?> <br />留言內容:<?php echo $sel->content?>
<a onClick="showdiv('contentid<?php echo $is;?>','showtext<?php echo $is;?>')" href="javascript:void(0)">回復</a>
<div id="contentid<?php echo $is;?>" class="none">
<?php
$query = $this->db->query("select * from message where son_id ='$sel->id' order by id");//獲取指定父id的子回復
$revis = $query->result();
foreach($revis as $row){?>
<p><?php if($row->sender_author == $row->receiver_author){echo $row->sender_author;}
else{ echo $row->sender_author."回復了:".$row->receiver_author;}?>
內容是:<?php echo $row->content?></p>
<?php }?>
<form action="<?php echo site_url()."/publish/son_message"?>" method="post">
<input name="son_idx" class="wole" value="<?php echo $sel->id?>" />
<input name="receiver_author" class="wole" value="<?php echo $sel->receiver_author;?>" />
<input class="wole" name="news_id" value="<?php echo $news_idx;?>" /><!--文章id-->
<textarea rows="5" cols="50" name="huifux"></textarea>
<br><input type="submit" name="sub" value="回復"></form></div></p>
<script language="JavaScript" type="text/JavaScript">
<!--
function showdiv(targetid,objN){
var target=document.getElementById(targetid);
var clicktext=document.getElementById(objN)
if (target.style.display=="block"){
target.style.display="none";
clicktext.innerText="回復";
} else {
target.style.display="block";
clicktext.innerText='收起';
}
}
-->
</script>
效果圖:
㈤ php如何清除html格式並去除文字中的空格然後截取文字
PHP清除html、css、js格式並去除空格的PHP函數
01 function cutstr_html($string,$length=0,$ellipsis='…'){
02 $string=strip_tags($string);
03 $string=preg_replace('/\n/is','',$string);
04 $string=preg_replace('/ |/is','',$string);
05 $string=preg_replace('//is','',$string);
06 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,$string);
07 if(is_array($string)&&!empty($string[0])){
08 if(is_numeric($length)&&$length){
09 $string=join('',array_slice($string[0],0,$length)).$ellipsis;
10 }else{
11 $string=implode('',$string[0]);
12 }
13 }else{
14 $string='';
15 }
16 return $string;
17 }
php 去除html標簽 js 和 css樣式
01 function clearHtml($content){
02 $content=preg_replace("/<a[^>]*>/i","",$content);
03 $content=preg_replace("/<\/a>/i","",$content);
04 $content=preg_replace("/<div[^>]*>/i","",$content);
05 $content=preg_replace("/<\/div>/i","",$content);
06 $content=preg_replace("/<!--[^>]*-->/i","",$content);//注釋內容
07 $content=preg_replace("/style=.+?['|\"]/i",'',$content);//去除樣式
08 $content=preg_replace("/class=.+?['|\"]/i",'',$content);//去除樣式
09 $content=preg_replace("/id=.+?['|\"]/i",'',$content);//去除樣式
10 $content=preg_replace("/lang=.+?['|\"]/i",'',$content);//去除樣式
11 $content=preg_replace("/width=.+?['|\"]/i",'',$content);//去除樣式
12 $content=preg_replace("/height=.+?['|\"]/i",'',$content);//去除樣式
13 $content=preg_replace("/border=.+?['|\"]/i",'',$content);//去除樣式
14 $content=preg_replace("/face=.+?['|\"]/i",'',$content);//去除樣式
15 $content=preg_replace("/face=.+?['|\"]/",'',$content);//去除樣式 只允許小寫 正則匹配沒有帶 i 參數
16 return $content;
17 }