Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to remove chinese characters in a string

Tags:

regex

php

is there any easy way to truncate chinese characters i found that regexp but it doesn't work as expected

<?php
$data1='疯狂的管道Test';
$data2='睡眠帮手-背景乐Test';

echo str_replace(preg_replace("/[^\x{4e00}-\x{9fa5}]+/u", '', $data1),'',$data1)
."<br>\n".
str_replace(preg_replace("/[^\x{4e00}-\x{9fa5}]+/u", '', $data2),'',$data2);
exit;

it works for data1 but not data2

like image 854
user2061745 Avatar asked Jul 29 '13 14:07

user2061745


People also ask

Is there a system to Chinese characters?

How does Chinese writing work? Unlike English, Chinese does not use an alphabet to record the written word; instead, it uses a system of ideogrammatic characters – 汉字 hànzì in Chinese. With this system, every character represents one syllable and each syllable has its own meaning.

How do I remove Chinese characters from Google?

Step one: At the bottom right, select the time. Step two: Select Settings Settings and then Advanced. Step three: In the "Languages and input" section, select Language. Step four: Next to the language you want to remove, select More More and then Remove.


1 Answers

You can use a Unicode character property (Han should work for you):

preg_replace("/\p{Han}+/u", '', $data)

Working example: http://ideone.com/uEiIV5

like image 61
Kobi Avatar answered Nov 04 '22 00:11

Kobi