Home php教程 php手册 通过正则过滤非法的汉字或字符

通过正则过滤非法的汉字或字符

Jun 06, 2016 pm 08:14 PM
character Influence show regular Chinese character filter pass

非法的汉字会影响到显示甚至程序的执行,会出现一些意想不到的结果。所以我们需要过滤这些非法的汉字或字符。 代码如下 function normalizeText($text, $length = null){ $text = \Normalizer::normalize($text, \Normalizer::FORM_C); $text = preg_replace

非法的汉字会影响到显示甚至程序的执行,会出现一些意想不到的结果。所以我们需要过滤这些非法的汉字或字符。
代码如下

function normalizeText($text, $length = null)
{
    $text = \Normalizer::normalize($text, \Normalizer::FORM_C);
    $text = preg_replace('/[^\p{L}\p{P}\p{N}\p{S}\p{Zs}]/u', "", $text);
    $text = preg_replace('/^\p{Z}*/u', "", $text);
    $text = preg_replace('/\p{Z}*$/u', "", $text);
    if ($length !== null) {
        $text = mb_substr($text, 0, $length, 'utf-8');
    }
    return $text;
}
Copy after login
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn

Hot Article Tags

Notepad++7.3.1

Notepad++7.3.1

Easy-to-use and free code editor

SublimeText3 Chinese version

SublimeText3 Chinese version

Chinese version, very easy to use

Zend Studio 13.0.1

Zend Studio 13.0.1

Powerful PHP integrated development environment

Dreamweaver CS6

Dreamweaver CS6

Visual web development tools

SublimeText3 Mac version

SublimeText3 Mac version

God-level code editing software (SublimeText3)

How effective is receiving phone calls using airplane mode? How effective is receiving phone calls using airplane mode? Feb 20, 2024 am 10:07 AM

How effective is receiving phone calls using airplane mode?

VBOX_E_OBJECT_NOT_FOUND(0x80bb0001)VirtualBox error VBOX_E_OBJECT_NOT_FOUND(0x80bb0001)VirtualBox error Mar 24, 2024 am 09:51 AM

VBOX_E_OBJECT_NOT_FOUND(0x80bb0001)VirtualBox error

Reasons and solutions for desktop layout being locked Reasons and solutions for desktop layout being locked Feb 19, 2024 pm 06:08 PM

Reasons and solutions for desktop layout being locked

How to open filtered duplicate files in Quark How to open filtered duplicate files in Quark Mar 01, 2024 am 11:25 AM

How to open filtered duplicate files in Quark

What problems will bad sectors on the hard drive cause? What problems will bad sectors on the hard drive cause? Feb 18, 2024 am 10:07 AM

What problems will bad sectors on the hard drive cause?

How to match multiple words or strings using Golang regular expression? How to match multiple words or strings using Golang regular expression? May 31, 2024 am 10:32 AM

How to match multiple words or strings using Golang regular expression?

How to turn off the comment function on TikTok? What happens after turning off the comment function on TikTok? How to turn off the comment function on TikTok? What happens after turning off the comment function on TikTok? Mar 23, 2024 pm 06:20 PM

How to turn off the comment function on TikTok? What happens after turning off the comment function on TikTok?

Correct way to display Chinese characters in matplotlib Correct way to display Chinese characters in matplotlib Jan 13, 2024 am 11:03 AM

Correct way to display Chinese characters in matplotlib

See all articles