在php中substr(),mb_substr()及mb_strcut三个函数都是字符截取函数,但是substr截取中文时会有乱码,而后两者支持中文截取,下面我来介绍介绍。
substr()函数
substr(string,start,length)
string表示要截取的对象,start表示从哪个位置开始截取,0表示从头开始,正数表示从这个数的位置后面截取,负数表示从结尾算开始截取的位置,但依然是从左到右截,length表示截取长度.负数表示排除或忽略结尾多少个字符.代码如下:
- <?php
- $siteurl = 'www.phpfensi.com';
- print_r (substr($siteurl,4));exit;
- //则返回: phpfensi.com表示从头第4个字符开始,返回后面的所有字符.
实例代码如下:
$siteurl = 'www.phpfensi.com';print_r (substr($siteurl,-6,2));exit;
如果要截取双字节的汉字.则要用PHP mb_substr函数或mb_strcut函数,但这两个函数依赖php扩展php_mbstring.dll组件,所以要配置你的服务器.即把php安装目录中的php_mbstring.dll文件复制到你的windows 2003 的c盘的Windows/system32目录中.
举个例子:
echo mb_substr('这样一来我的字符串就不会有乱码^_^', 0, 7, 'utf-8');
输出:这样一来我的字,代码如下
输出:这样一
从上面的例子可以看出,mb_substr是按字来切分字符,而mb_strcut是按字节来切分字符,但是都不会产生半个字符的现象……代码如下:
- <?php
- echo mb_substr('飞花院博客feihuayuan',0,9);
- //返回: 飞花院
- echo mb_substr('飞花院博客feihuayuan',0,9,'utf-8');
- //则返回: 飞花院博客feih
- mb_strcut('飞花院博客feihuayuan',0,9,'utf-8');
- 则返回:飞花院
- ?>
再举个例子,有一段文字,分别用mb_substr和mb_strcut来做切分,代码如下:
- <?php
- $str = '我是一串比较长的中文-';
- echo "mb_substr:" . mb_substr($str, 0, 6, 'utf-8');
- echo "<br>";
- echo "mb_strcut:" . mb_strcut($str, 0, 6, 'utf-8');
- ?>
- /*输出结果如下:
- mb_substr:我是一串比较
- mb_strcut:我是
- */
本文章总结:从上面实例可以看得出来,substr只支持单字节的,这样只适合英文截取,而mb_substr函数是双字体截取了,正好可用于中文了,而mb_strcut是三字节了。
波比源码 » substr(),mb_substr()及mb_strcut函数用法与区别
order levofloxacin 500mg sale oral levaquin
order sildenafil 100mg pill brand robaxin buy methocarbamol pills
buy generic lamotrigine 200mg buy mebendazole sale buy retin gel online cheap
buy cialis 10mg cialis by mail real viagra pills
buy arimidex 1 mg sale Buy cheap viagra now viagra professional
isotretinoin 20mg canada azithromycin 250mg tablet stromectol order
buy isosorbide 40mg for sale buy micardis 20mg pills order generic telmisartan 80mg
fluvoxamine 50mg generic buy luvox 100mg generic glucotrol cheap
order itraconazole 100 mg generic order tindamax 500mg for sale cheap tinidazole 300mg
buy clozapine 100mg buy ipratropium online cheap generic decadron
cost omeprazole help writing paper best online gambling
cost tadalafil 20mg tadalafil order sildenafil order online
triamcinolone 4mg without prescription claritin 10mg cost desloratadine ca
cialis price walmart inderal 10mg generic order plavix 75mg online cheap
buy metronidazole generic clavulanate price order bactrim 960mg generic
buy fluconazole online viagra cost viagra brand
best casino slots online free spins no deposit order tadalafil 40mg online
real online gambling oral tadalafil 40mg cialis 5mg price
order prednisone 20mg for sale deltasone 5mg us order vermox pills
tadacip 20mg tablet diclofenac cheap order indocin 75mg pill
terbinafine tablet buy terbinafine generic amoxicillin price
naproxen 500mg pills buy naproxen 250mg for sale purchase prevacid for sale
order clarithromycin 250mg generic purchase antivert sale order meclizine 25mg generic
order albuterol sale order cipro 500mg sale buy ciprofloxacin 1000mg pill
order montelukast pill sildenafil 50mg generic order viagra
buy cialis 40mg for sale Buy cialis without a prescription best online casino for real money
order cialis 10mg for sale Free cialis samples buy tadalafil 5mg online
ivermectin for humans purchase symmetrel generic dapsone
real casino slot machine games blackjack card game card games online
buy ramipril 10mg sale oral glimepiride 1mg cheap arcoxia 120mg
how to write a letter to a hiring manager buy generic azulfidine 500mg buy azulfidine online
purchase acetazolamide pill buy acetazolamide 250 mg generic imuran 25mg sale
buy digoxin 250mg generic order telmisartan sale molnunat 200mg uk
carvedilol 25mg tablet oxybutynin 2.5mg usa buy amitriptyline 50mg online
order amoxil pill order amoxil 250mg for sale ivermectin dosage
fosamax 70mg canada ibuprofen 600mg without prescription motrin price
dapoxetine 30mg generic buy domperidone sale buy motilium
nortriptyline online buy buy paracetamol 500mg sale paroxetine price
tricor ca order fenofibrate 200mg pills purchase sildenafil without prescription
order tadalafil 20mg pill viagra generic sildenafil 100mg without prescription
nexium canada buy esomeprazole 20mg online lasix 100mg over the counter
After all, what a great site and informative posts, I will upload inbound link – bookmark this web site? Regards, Reader. cami halısı
buy generic tadalafil 5mg buy levitra cheap ed pills
sildenafil 100mg tablet buy lyrica 150mg for sale finasteride 5mg over the counter
buy accutane 20mg sale buy acillin without prescription buy ampicillin 500mg
buy antihistamine pills generic levothyroxine amoxiclav oral
provigil 100mg tablet lisinopril 10mg cost metoprolol price
order generic prednisolone 5mg order generic lasix order generic furosemide
brand azathioprine 50mg buy naproxen 500mg online order naproxen 250mg sale
monodox without prescription zovirax 400mg pills zovirax 800mg for sale
cheap omnicef 300 mg order omnicef 300 mg online pantoprazole 20mg pill
avlosulfon 100mg us dapsone 100 mg drug atenolol 100mg pill
order simvastatin 20mg online cheap order phenergan pills sildenafil citrate 50mg
zetia canada zetia online buy order methotrexate 2.5mg pills
coumadin 5mg price order reglan generic order allopurinol pill
order generic lipitor 10mg viagra for women sildenafil next day delivery usa
femara 2.5mg price us pharmacy viagra cheap viagra pills
how to buy isotretinoin buy generic zithromax for sale order zithromax 250mg generic
order levothyroxine generic order generic synthroid 150mcg buy levitra without a prescription
order praziquantel for sale purchase microzide generic periactin 4 mg us
buy lyrica 150mg sale buy cheap generic lyrica how to buy priligy
buy toradol without a prescription buy colcrys 0.5mg sale order inderal 10mg without prescription
zantac order online buy cheap ranitidine buy celecoxib 100mg generic
plavix 150mg without prescription plavix 150mg pills nizoral 200 mg brand
canadian cialis buy cialis without prescription buy viagra 50mg pills
cheap cefadroxil order finasteride 5mg online cheap order proscar 1mg generic
buy generic diflucan buy ciprofloxacin pill order cipro 1000mg for sale
nolvadex 10mg cost buy nolvadex 10mg generic order ceftin 250mg online cheap
buy suhagra 100mg sale purchase sildalis sildalis order online
minomycin oral order minocycline 50mg sale actos 30mg us
brand arava 10mg buy cheap generic azulfidine azulfidine cost
isotretinoin 40mg over the counter order amoxil 1000mg pills order zithromax online
oral azithromycin azipro usa buy neurontin 800mg generic
lasix for sale online buy vibra-tabs pill order albuterol 4mg pills
buy generic vardenafil over the counter order levitra 10mg online plaquenil 200mg oral
order mesalamine generic asacol 400mg over the counter irbesartan uk
olmesartan order online buy calan generic divalproex us
buy coreg for sale cost coreg aralen 250mg uk
olumiant 2mg tablet olumiant online lipitor 10mg pill
buy singulair no prescription buy montelukast pills for sale avlosulfon 100mg sale
adalat 30mg over the counter order perindopril 8mg online cheap order allegra 120mg generic