Be part of JetBrains PHPverse 2026 on June 9 – a free online event bringing PHP devs worldwide together.

Crazylife's avatar

How to replace or escape all chinese character from a string?

I tried the code below

str = 'Arc's (a chinese character here)'
str.replace(/[^a-z\d\s]+/gi,'');

The result show

Arc039s

Expected result

Arc's

What's wrong with the code, and is that the right way for removing chinese character including punctuation?

Thanks in advance.

0 likes
5 replies
impbob36's avatar

I tried your code in Chrome javascript console but mine shows correctly

str = "Arc's 漢字"

$a = str.replace(/[^a-z\d\s]+/gi,'')
console.log($a) // "Arcs "

The 039s looks like it's displaying the unicode number. You don't mention where or how you are printing it.

If your looking for a regex solution in PHP, I use something like this for Japanese:

$str = "Arc's 漢字"
$new = preg_replace("/(\p{Z}|\p{P}|\p{Han}|\p{Katakana}|\p{Hiragana})+/u", '', $str )

// $this->assertEquals("Arc's", $new );

This says to replaces all

  • space characters (\p{Z}),
  • punctuation (\p{P}),
  • Chinese characters (\p{Han}),
  • Japanese hiragana (\p{Hiragana})
  • Japanese katakana (\p{Katakana}) ... using unicode (/u) .. with nothing ('')

Laravel has a string->slug function that strips all punctuation and non alphanumeric characters, and lower cases the string suitable for urls

$str = "Arc's 漢字";
// $this->assertEquals('arcs', str_slug($str ));
Crazylife's avatar

@impbob Sorry, forgot to mention where i am going to display the output. I am sharing some text via web 'whatsapp' sharing button. When i am sharing the text to the chat, it show incorrect result. By right, everything should be worked. I have no idea and finding solution.

impbob36's avatar

Not familiar with 'whatsapp'. The 039 code is for the ' (single quote) which is what is catching you up.

Crazylife's avatar

The problem occurred if retrieve the data from my database, and it will show apostrophe as ' . Anyone know how to solve this?

Please or to participate in this conversation.