本文章分析了在php中正则表达式对中文的支持问题,一般情况下我们可会能简单利用我上面的正则来验证中文,后面我分析了关于兼容gb2312,utf-8和表达式 ^[x7f-xff]+$ 可以完全搞定中文不兼容的问题。
- $str = '中文123456789abcdefg';
- echo preg_match("/^[u4e00-u9fa5_a-zA-Z0-9]{3,15}$",$strName);
运行一下上面这段代码,看会有什么提示信息?
Warning: preg_match(): Compilation failed: PCRE does not support L, l, N, P, p, U, u, or X at offset 3 in F:wwwrootphptest.php on line 2
原来,PHP正则表达式中不支持下列 Perl 转义序列:L, l, N, P, p, U, u, or X,在 UTF-8 模式下,允许用“x{…}”,花括号中的内容是表示十六进制数字的字符串,原来的十六进制转义序列 xhh 如果其值大于 127 的话则匹配了一个双字节 UTF-8 字符,所以我们可以这样来解决:
- preg_match("/^[x80-xff_a-zA-Z0-9]{3,15}$",$strName);
- preg_match('/[x{2460}-x{2468}]/u', $str);
匹配内码汉字,按照他提供的方式进行测试,代码如下:
- $str = "php编程";
- if (preg_match("/^[x{2460}-x{2468}]+$/u",$str)) {
- print("该字符串全部是中文");
- } else {
- print("该字符串不全部是中文");
- }
发现这次依然对是否为中文判断失常,不过,既然x表示的十六进制数据,为什么和js里边提供的范围x4e00-x9fa5不一样呢?于是我就换成了下边的代码:
- $str = "php编程";
- if (preg_match("/^[x4e00-x9fa5]+$/u",$str)) {
- print("该字符串全部是中文");
- } else {
- print("该字符串不全部是中文");
- }
本来以为铁定成功了的事情,没想到,warning又一次产生了:Warning: preg_match() [function.preg-match]: Compilation failed: invalid UTF-8 string at offset 6 in test.php on line 3
看来又有错误的表达方式了,于是对照了一下那篇文章的表达方式,给“4e00”和“9fa5”两边分别用"{"和“}”包起来,跑了一遍,发现真的准确了:
- $str = "php编程";
- if (preg_match("/^[x{4e00}-x{9fa5}]+$/u",$str)) {
- print("该字符串全部是中文");
- } else {
- print("该字符串不全部是中文");
- }
知道了php中utf-8编码下用正则表达式匹配汉字的最终正确表达式——/^[x{4e00}-x{9fa5}]+$/u,最后总结出:
- //if (preg_match(“/^[".chr(0xa1)."-".chr(0xff)."]+$/”, $str)) { //只能在GB2312情况下使用
- if (preg_match(“/^[x7f-xff]+$/”, $str)) { //兼容gb2312,utf-8
- echo “正确输入”;
- } else {
- echo “错误输入”;
- }
双字节字符编码范围
1. GBK (GB2312/GB18030)
x00-xff GBK双字节编码范围
x20-x7f ASCII
xa1-xff 中文 gb2312
x80-xff 中文 gbk
2. UTF-8 (Unicode)
u4e00-u9fa5 (中文)
xAC00-xD7A3 (韩文)
u0800-u4e00 (日文)
波比源码 » php正则表达匹配中文问题分析
order levaquin 500mg pill order levaquin 500mg
oral lamictal 50mg minipress 1mg brand tretinoin cream tablet
isotretinoin 20mg sale zithromax pills ivermectin 12 mg tablets for humans
order clomiphene 50mg without prescription albuterol uk no deposit casino
us pharmacy cialis buy sildenafil without prescription order sildenafil 50mg for sale
buy ozobax pills brand toradol 10mg cheap ketorolac
azithromycin 500mg for sale neurontin price cheap generic neurontin
real money casino app best casino order cialis 10mg pill
lasix 100mg generic lasix 40mg without prescription plaquenil canada
brand sildenafil 100mg tamoxifen 20mg for sale generic budesonide
order naprosyn 250mg online order naprosyn online cheap prevacid cost
proventil 100 mcg sale proventil 100 mcg ca buy ciprofloxacin
order montelukast 10mg online brand sildenafil viagra 100mg generic
tadalafil drug Generic cialis online tadalafil 20mg sale
stromectol us avlosulfon 100mg without prescription order avlosulfon pills
online roulette for real money free casino games cheap custom essays
mesalamine for sale online buy astelin sprayers oral irbesartan 300mg
purchase temovate sale temovate drug cordarone online buy
buy coreg generic amitriptyline oral oral amitriptyline 10mg
buy dapoxetine 30mg generic order priligy 60mg for sale buy domperidone pill
alendronate cost furadantin over the counter motrin us
pepcid 20mg for sale buy pepcid 40mg generic buy mirtazapine 30mg without prescription
ropinirole ca purchase labetalol sale trandate 100mg cost
Thank you great posting about essential oil. Hello Administ . cami halısı
cialis discount Cialis store natural pills for erectile dysfunction
accutane 40mg brand order prednisone 5mg pills ampicillin 500mg without prescription
isotretinoin tablet zithromax over the counter order zithromax pill
prednisolone 10mg us prednisolone 5mg brand lasix 100mg tablet
generic uroxatral diltiazem us oral diltiazem 180mg
buy sildenafil 50mg online sildenafil 50mg tablets cialis 40mg pill
buy levaquin 250mg sale order zyban 150mg pill bupropion 150 mg drug
order cetirizine 10mg for sale buy zyrtec 5mg pill buy zoloft 50mg online cheap
escitalopram 10mg cheap generic prozac 20mg revia 50mg generic
order femara 2.5mg femara 2.5 mg brand sildenafil 100mg for sale
tadalafil over the counter cialis pill male erection pills
stromectol 3 mg dosage order isotretinoin 40mg without prescription order isotretinoin 20mg sale
buy amoxil 500mg pill where can i buy azithromycin buy prednisolone 20mg pills
order isotretinoin 40mg pill buy azithromycin 500mg online cheap zithromax 250mg uk
purchase ventolin inhalator generic synthroid 75mcg for sale levoxyl online
clomid order hydroxychloroquine for sale buy cheap hydroxychloroquine
generic vibra-tabs buy ventolin inhalator generic order amoxiclav pill
order zestril 5mg pill zestril pill buy metoprolol 100mg
lyrica 75mg over the counter priligy over the counter dapoxetine without prescription
order generic xenical 120mg order allopurinol 100mg generic order allopurinol 300mg
rosuvastatin brand order zetia online cheap buy domperidone without prescription
order tetracycline online buy flexeril 15mg online buy baclofen 25mg
buy generic plavix over the counter nizoral 200 mg brand nizoral for sale online
flomax order where to buy aldactone without a prescription buy spironolactone pills for sale
buy duloxetine 40mg order piracetam 800 mg generic nootropil brand
oral ipratropium 100 mcg buy linezolid cheap zyvox online
order progesterone 200mg without prescription order zyprexa pills olanzapine cheap
cheap nebivolol 5mg purchase clozapine sale cheap clozapine 50mg
nateglinide 120 mg pills buy generic atacand 8mg order candesartan 8mg sale
purchase simvastatin buy simvastatin generic female viagra sildenafil
tegretol 200mg pills buy carbamazepine without prescription lincocin 500mg brand
order estradiol generic order estradiol 1mg minipress sale
order diflucan 200mg how to get ampicillin without a prescription buy ciprofloxacin pill
brand mebendazole 100mg retin gel over the counter cheap tadalafil 10mg
purchase avanafil without prescription purchase tadacip without prescription diclofenac 100mg tablet
purchase cleocin online order cleocin without prescription best pills for ed
order indomethacin without prescription indomethacin 75mg usa buy generic suprax for sale
nolvadex ca rhinocort oral order ceftin pill
purchase careprost online cheap order desyrel generic desyrel 50mg pill
buy cheap arava order sulfasalazine 500 mg online purchase sulfasalazine online cheap
order isotretinoin 40mg pill buy generic amoxicillin over the counter zithromax 500mg without prescription
order tadalafil generic purchase sildenafil pill purchasing cialis on the internet
azipro pill prednisolone 40mg us order gabapentin 800mg online cheap
buy furosemide 40mg pills order monodox for sale buy ventolin pill
altace pill order etoricoxib 120mg without prescription etoricoxib online buy
vardenafil 10mg cost levitra for sale buy hydroxychloroquine 400mg online
asacol 400mg cheap avapro online buy irbesartan 150mg pill
levitra 20mg uk order plaquenil 400mg online cheap order hydroxychloroquine generic
Piperlongumine piplartine and analogues Antiproliferative microtubule destabilising agents generic 5mg cialis best price 9 after in- vitro fertilization, suggesting that clinicians who manage infertility in these patients do not wait 12 months until attempting conception
olmesartan without prescription divalproex pills divalproex us
order generic diamox 250 mg buy diamox cheap cost azathioprine 25mg
order lanoxin online cheap molnupiravir generic molnunat
naprosyn 500mg oral cefdinir online order order lansoprazole pills
nifedipine 30mg for sale allegra where to buy oral fexofenadine 120mg
dapoxetine 90mg cheap cytotec pills order orlistat 120mg generic
buy diltiazem online order zovirax 400mg pill zyloprim for sale
buy rosuvastatin 20mg generic order generic zetia buy motilium 10mg generic
cheap acillin order cipro 1000mg flagyl 400mg cheap
tetracycline order online cyclobenzaprine 15mg tablet order generic baclofen 10mg
buy erythromycin 500mg without prescription erythromycin price buy nolvadex no prescription
buy topiramate 100mg online buy levofloxacin 250mg online cheap where to buy levaquin without a prescription
buy robaxin no prescription suhagra cost buy sildenafil generic
purchase dutasteride sale dutasteride canada order mobic 7.5mg generic
order sildenafil 100mg generic order estrace online cheap buy estrace 2mg without prescription
celebrex 100mg canada buy generic flomax 0.4mg order zofran pill
cialis for sale online 7 ОјM, meloxicam K i 50
lamictal 200mg canada order lamictal 200mg pill minipress 2mg us
spironolactone 25mg sale aldactone 25mg cost brand valtrex 500mg
buy tretinoin gel sale tadalafil over the counter order avana
cheap finasteride cheap viagra 100mg buy sildenafil without prescription
cost tadacip oral indocin 75mg purchase indocin pills
cost tadalafil buy diflucan sale buy ed pills gb
buy azathioprine without a prescription buy azathioprine 25mg online micardis 80mg ca
buy erectile dysfunction drugs viagra online buy buy viagra online
non prescription erection pills purchase cialis for sale cialis professional
buy pyridium 200mg sale pyridium 200 mg over the counter amantadine over the counter
allegra us buy glimepiride online cost amaryl 1mg
buy generic hytrin order hytrin 1mg generic tadalafil 20mg pills
arcoxia order online buy etoricoxib tablets buy astelin nasal spray
avapro 150mg drug buy buspar without prescription buy generic buspar for sale
ditropan 5mg price fosamax oral brand alendronate 35mg
order biltricide order microzide online cheap cyproheptadine 4 mg brand
buy glipizide sale glipizide 10mg oral buy betamethasone without a prescription
anacin order online acetaminophen 500 mg ca pepcid medication
clomipramine pills order sporanox 100mg online prometrium pill
valsartan 80mg cheap buy diovan 80mg online order generic combivent 100 mcg
decadron uk order nateglinide generic buy nateglinide 120 mg online
zyban 150 mg pill bupropion order online buy atomoxetine without a prescription
buy captopril 25 mg candesartan canada order carbamazepine generic
quetiapine for sale online buy escitalopram 20mg generic cheap lexapro 20mg
buy generic combivir order epivir generic buy quinapril no prescription
buy fluoxetine generic order naltrexone 50 mg without prescription where to buy femara without a prescription
order frumil 5 mg online cheap order differin zovirax for sale
buy bisoprolol 10mg online cheap myambutol 600mg brand order terramycin for sale