PHPFixing
  • Privacy Policy
  • TOS
  • Ask Question
  • Contact Us
  • Home
  • PHP
  • Programming
  • SQL Injection
  • Web3.0

Sunday, November 20, 2022

[FIXED] How to replace all non-alphabetic characters with UTF-8 support in PHP

 November 20, 2022     php, preg-replace, regex, utf-8     No comments   

Issue

I want to remove all non-alphabetic character from a string. The problem is that I don't know the letter range because it is UTF8 string.

It can be ENGLISH, ՀԱՅԵՐԵՆ, ქართული, УКРАЇНСЬКИЙ, РУССКИЙ

I usually do something like this:

$str = preg_replace('/[^a-zA-Z]/', '', $str);

or

$str = preg_replace('/[^\w]/u', '', $str);

but they both clear foreign characters.

Any ideas?


Solution

UPDATE: As for Unicode, RegExp will look like this [^\p{L}\s]+ (without replacing spaces)

It will replace all non-alpha characters with UTF8 support.

  • \P{L}+ - matches any non-letter symbols
  • \p{P}+ - removes punctuation only

Here are some reference docs that can be helpful:

  • Tutorial on RegExp UTF8
  • Unicode character properties


Answered By - Paul T. Rawkeen
Answer Checked By - Pedro (PHPFixing Volunteer)
  • Share This:  
  •  Facebook
  •  Twitter
  •  Stumble
  •  Digg
Newer Post Older Post Home

0 Comments:

Post a Comment

Note: Only a member of this blog may post a comment.

Total Pageviews

Featured Post

Why Learn PHP Programming

Why Learn PHP Programming A widely-used open source scripting language PHP is one of the most popular programming languages in the world. It...

Subscribe To

Posts
Atom
Posts
Comments
Atom
Comments

Copyright © PHPFixing