導航:首頁 > 編程語言 > php省略字元

php省略字元

發布時間:2022-11-14 10:17:27

php 字元串截取 加省略號

自己看看下面的內容,是pHP的字元串截取,多種情況。
-------------------------
怎麼會沒有符合的呢?先取你所要的長度,然後返回你所要的字元串,在進行相應的連接操作。

❷ php中數據內容過長怎麼用省略號表示

echo substr($a,0,7)."...";
echo substr_replace("Hello world","...",6);
因為中英文字元數目不一樣,建議寫一個方法類進行邏輯判斷

❸ PHP指字元數量截斷,在後面加上省略號。

substr可以截斷字元串,但是不能顯示省略號,用 Smarty 的的 truncate 方法可以把頁面的字元串截斷並顯示省略號。

❹ 如何去掉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替換字元串中間字元為省略號的方法。分享給大家供大家參考。具體分析如下:
對於一個長字元串,如果你只希望用戶看到頭尾的部分內容,隱藏掉中間內容,你可以使用這個php函數,他可以指定要隱藏掉的中間字元串的數量
/**
*
Rece
a
string
by
the
middle,
keeps
whole
words
together
*
*
@param
string
$string
*
@param
int
$max
(default
50)
*
@param
string
$replacement
(default
[...])
*
@return
string
*
@author
david
at
ethinkn
dot
com
*
@author
loic
at
xhtml
dot
ne
*
@author
arne
dot
hartherz
at
gmx
dot
net
*/
function
strMiddleReceWordSensitive($string,$max=50,$rep='[...]'){
$strlen
=
strlen($string);
if
($strlen
<=
$max)
return
$string;
$lengthtokeep
=
$max
-
strlen($rep);
$start
=
0;
$end
=
0;
if
(($lengthtokeep
%
2)
==
0)
{
$start
=
$lengthtokeep
/
2;
$end
=
$start;
}
else
{
$start
=
intval($lengthtokeep
/
2);
$end
=
$start
+
1;
}
$i
=
$start;
$tmp_string
=
$string;
while
($i
<
$strlen)
{
if
(isset($tmp_string[$i])
and
$tmp_string[$i]
==
'
')
{
$tmp_string
=
substr($tmp_string,
0,
$i)
.
$rep;
$return
=
$tmp_string;
}
$i++;
}
$i
=
$end;
$tmp_string
=
strrev
($string);
while
($i
<
$strlen)
{
if
(isset($tmp_string[$i])
and
$tmp_string[$i]
==
'
')
{
$tmp_string
=
substr($tmp_string,
0,
$i);
$return
.=
strrev
($tmp_string);
}
$i++;
}
return
$return;
return
substr($string,
0,
$start).$rep.substr($string,
-
$end);
}
演示範例:
//
example:
$text
=
'This
is
a
very
long
test
sentence,
bla
foo
bar
nothing';
print
strMiddleReceWordSensitive
($text,
30)
.
"\n";
//
Returns:
This
is
a
very[...]foo
bar
nothing
(~
30
chrs)
print
strMiddleReceWordSensitive
($text,
30,
'...')
.
"\n";
//
Returns:
This
is
a
very...foo
bar
nothing
(~
30
chrs)
希望本文所述對大家的php程序設計有所幫助。

❻ PHP 中怎樣移除節制字元

"t" - tab
"n" - new line
"x0b" - 縱向列表符
"r" - 回車
" " - 普通空白字元
以及用戶指定字元哦。
php教程
定義和用法
trim() 函數從字元串的兩端刪除空白字元和其他預定義字元。
可選。規定要轉換的字元串。如果省略該參數,則刪除以下所有字元:
"�" - null
"t" - tab
"n" - new line
"x0b" - 縱向列表符
"r" - 回車
" " - 普通空白字元
$str = " this line containstliberal rn use of whitespace.nn";
// first remove the leading/trailing whitespace
//去掉開始和結束的空白
$str = trim($str);
// now remove any doubled-up whitespace
//去掉跟隨別的擠在一塊的空白
$str = preg_replace('/s(?=s)/', '', $str);
// finally, replace any non-space whitespace, with a space
//最後,去掉非space 的空白,用一個空格代替
$str = preg_replace('/[nrt]/', ' ', $str);
echo "
{$str}
";

//
$str = "##使用函數trim去掉字元串兩端特定字元####";
$str1 = trim($str,"#"); //為函數trim傳入第二個參數,trim將刪除字元串$str兩端的#字元
echo $str."
";
echo $str1;
?>
?>

❼ 如何去掉一個字元串中某個字元 php

用字元串替換就行了,將你想換掉的字元替換為空就可以了。
比如說:
你原來的字元串是 "abcdefg",想將其中的a去掉,則可以用以下方法
$str="abcdefg";
$str=str_replace('a','',$str);
echo $str;
希望可以幫到你。

❽ PHP,查詢資料庫的字過長,怎麼省略一部分用其他來代替

參考PHPmb_substr()中文字元串截取函數

$content=mb_substr($str,0,21,'utf-8').'......';

❾ php顯示指定長度的字元串,超出長度以省略號填補尾部

例子1,使用strlen獲得長度,substr提取前5個字元:

$str='123456789';
if(strlen($str)>5)$str=substr($str,0,5).'...';
echo$str;

例子2,使用explode拆分IP,然後組裝:

$ip='192.168.0.1';
list($ip1,$ip2,$ip3)=explode('.',$ip);
$ip="*-*-$ip3-$ip4";
echo$ip;

❿ php中怎樣把過長的字元只顯示規定的幾個後面用省略號代替

寫個測試:

<?php
functioncut_str($str,$len,$suffix="..."){
if(function_exists('mb_substr')){
if(strlen($str)>$len){
$str=mb_substr($str,0,$len).$suffix;
}
return$str;
}else{
if(strlen($str)>$len){
$str=substr($str,0,$len).$suffix;
}
return$str;
}
}

應該能滿足簡單的需求

閱讀全文

與php省略字元相關的資料

熱點內容
銀河v10驅動重編譯 瀏覽:889
電腦上文件夾右擊就會崩潰 瀏覽:689
右美維持演算法 瀏覽:938
php基礎編程教程pdf 瀏覽:219
穿越之命令與征服將軍 瀏覽:351
android廣播重復 瀏覽:832
像阿里雲一樣的伺服器 瀏覽:318
水冷空調有壓縮機嗎 瀏覽:478
訪問日本伺服器可以做什麼 瀏覽:432
bytejava詳解 瀏覽:448
androidjava7 瀏覽:385
伺服器在山洞裡為什麼還有油 瀏覽:886
天天基金app在哪裡下載 瀏覽:974
伺服器軟路由怎麼做 瀏覽:292
冰箱壓縮機出口 瀏覽:228
OPT最佳頁面置換演算法 瀏覽:645
網盤忘記解壓碼怎麼辦 瀏覽:853
文件加密看不到裡面的內容 瀏覽:654
程序員腦子里都想什麼 瀏覽:434
oppp手機信任app在哪裡設置 瀏覽:189