Business
Jobs
  • About Us
  • Solutions
    • Job Postings
      Post your job and receive qualified candidates in 48h.
    • Candidate Assessments
      500+ technical and psychological tests, plus anti-fraud.
    • Headhunting
      Tailor-made executive search from start to finish.
    • Payroll + EOR
      Payroll dispersal and EOR across 15+ LATAM countries.
  • Pricing
  • Jobs

0

426
Views
php regex: how to match number + letters + Chinese

I am trying to handle the string which got letters, numbers, Chinese and some punctuations and left number, letters and Chinese only like below

raw string

a>b%%c##1@23测$$试??\\:.##,,??!!

result

abc123测试

for Chinese, preg_replace('/\P{Han}+/u', '', $text) works perfectly

测试 // result

and for number and letters, preg_replace('/[^0-9a-zA-Z]/', '', $text) works too.

abc123 // result

But how can I combine them?

why preg_replace('/[\P{Han}]|[0-9a-zA-Z]/u', '', $raw); doesn't work as expected?

Thanks a lot for anyone help!

over 4 years ago · Santiago Trujillo
1 answers
Answer question

0

You need

preg_replace('~[^0-9a-zA-Z\p{Han}]+~u', '', $raw)

See the regex demo.

The [^0-9a-zA-Z\p{Han}]+ is a negated character class that matches any one or more chars other than ASCII digits, ASCII letters and any Chinese chars.

It is important to use the u flag with this pattern, as your input is Unicode strings.

See the PHP demo:

$raw = 'a>b%%c##1@23测$$试??\\:.##,,??!!';
echo preg_replace('~[^0-9a-zA-Z\p{Han}]+~u', '', $raw);
// => abc123测试
over 4 years ago · Santiago Trujillo Report
Answer question
Find remote jobs

Discover the new way to find a job!

Top jobs
Top job categories
Business
Post vacancy Pricing Sales
Legal
Terms and conditions Privacy policy
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Show me some job opportunities
There's an error!