How can I extract or preg_replace chinese characters in a string?

随声附和 提交于 2020-01-17 08:55:20

问题


I am currently have a list of string like this

蘋果,香蕉,橙。
榴蓮, 啤梨
鳳爪,排骨,雞排
24個男,2個女,30個老人

What I want to do is just explode all chinese and alphanumeric character from these strings. How can I replace all special characters like , / " and spaces with - or _

then extract all chinese character with explode() like $str = explode("-",$str); or $str = explode("_",$str); ?

I am currently have a RegEx like this

 if(/^\S[\u0391-\uFFE5 \w]+\S$/.test(value)).....

And I modified it into

 $str = preg_replace("/^\S[\x{0391}-\x{FFE5} \w]+\s+\S$/u", "-", $str);

but it seems it didn't work...

the online exampls: https://www.regex101.com/r/qR8aA6/1

EDIT : my expected output(for the first sting):

firstly it should be replaced into

蘋果-香蕉-橙- or 蘋果_香蕉_橙_

then I can use $str = explode("-",$str); to make them finally become:

Array
    (
     [0] => 蘋果
     [1] => 香蕉
     [2] => 橙
     )

回答1:


Seems like you want something like this,

$txt = <<<EOT
蘋果,香蕉,橙。
榴蓮, 啤梨
鳳爪,排骨,雞排
24個男,2個女,30個老人
EOT;
echo preg_replace('~[^\p{L}\p{N}\n]+~u', '-', $txt);

Output:

蘋果-香蕉-橙-
榴蓮-啤梨
鳳爪-排骨-雞排
24個男-2個女-30個老人

DEMO

Explanation:

  • \p{L} Matches any kind of letter from any language.
  • \p{N} matches any kind of numeric character in any script.
  • \n Matches a newline character.
  • By putting all inside a negated character class will do the opposite operation.


来源:https://stackoverflow.com/questions/28357638/how-can-i-extract-or-preg-replace-chinese-characters-in-a-string

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!