PHP正则表达式可解密英文和中文字符 [英] PHP regex to to decipher English and Chinese characters

查看:92
本文介绍了PHP正则表达式可解密英文和中文字符的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个英文和中文的描述。

I have a description which is in English and Chinese.

我如何使用正则表达式来表示类似的内容,如果该行包含中文字符,那么A,否则B?

How would I go about using regex to say something like, if the line contains a Chinese character then do A, else do B ?

此处的示例

电源: 110V/220W50-60HZ
功率:60W
光源:12V 150 W
尺寸:220x150x280mm
重量:2.3KG



Voltage : 110V/220W50-60HZ
Power : 60W
Bulb : 12V 150 W
Size : 220x150x280mm
Weight:2.3KG


推荐答案

汉字在以下范围内: U + 4E00..U + 9FFF

如果您的expreg扩展程序是在Unicde支持下构建的,则 b\p {InCJK_Unified_Ideographs} 很好地替代了 [\x {4E00} -\x {9FFF}] (Jens Struwe给出的链接)。

If your expreg extension has been built with Unicde support, b\p{InCJK_Unified_Ideographs} is a good replacement for [\x{4E00}-\x{9FFF}] (which was in the link Jens Struwe gave).

您可以在此处找到大多数(全部?)Unicode范围: http://www.regular-expressions。 info / unicode.html

You can find most (all?) of Unicode ranges here: http://www.regular-expressions.info/unicode.html

我不确定您要实现什么目标,但也许一个好的开始是将您的描述按行分开。然后,针对每一行,查找是否为中文,然后运行适当的正则表达式。 ;)

I'm not sure what you want to achieve, but maybe a good start would be split your description by line. Then, for each line, find whether it's Chinese or not, and run the appropriate regex. ;)

这篇关于PHP正则表达式可解密英文和中文字符的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆