正则 PHP
<?php//中文编码包含UTF-8,GBK$str = "新_建&文\件,夹 abd";//获取结果$res = "新建文件夹abd";//包含非法字符:$out[0] = "_";$out[1] = "&";$out[2] = "\";$out[3] = " ";$out[4] = ",";?>
回复讨论(解决方案)
$str = "新_建&文\件,夹 abd";echo preg_replace('/[_&\\\,\s]+/u','',$str);
//中文编码包含UTF-8,GBK$str = "新_建&文\件,夹 abd";//获取结果$res = "新建文件夹abd";//包含非法字符:$out[0] = "_";$out[1] = "&";$out[2] = "\\";$out[3] = " ";$out[4] = ",";$pattern = join('|', array_map('preg_quote', $out));echo preg_replace("/$pattern/", '', $str);新建文件夹abd
不是吧?猩猩也问这问题?
#1,#2的方法可行,但是治标不治本;
我没有说清楚吧,是这样,$out是过滤出来的字符,之前是不知道的,我只是举个例子有这些非法字符。
实际目标:在获取input的表单结果后,将除中文和英文字母以外的所有非法字符扣掉。
如果单纯的英文字母还好确定范围,符合要求的有[A-Za-z0-9],也就是之外的全部为非法字符。
但是如果加上中英文混排,除非你确定出所有的汉字范围,否则没有好的方法扣除掉,例如GBK通常可以用[\x80-\xff][\x40-\xfe]来表示汉字的范围,注意这仅仅是大概范围,如果是utf8的话,则是\u4e00-\u9fa5,这里面如果有一些你认为是非法字符的话,是没有办法干掉的。
粗略的说,gbk非中文和英文字母以外的所有非法字符的范围应该是[^A-Za-z0-9\x80-\xff\x40-\xfe]
而utf8的则是[^A-Za-z0-9\x{4e00}-\x{9fff}]
echo preg_replace('/[^A-Za-z\p{Han}]+/u', '', "新_建&文\件,夹 abd");
上边的gbk的正则写错了,看例子就额可以知道
<?php //源文件文件gbk编码$test = '新_建&文\件,夹a啊[?[ abd';$matches = array();$reg = "/[^a-zA-Z0-9\x80-\xff]/";preg_match_all($reg, $test, $matches);var_dump($matches);$str = '?';var_dump(ord($str[0]));var_dump(ord($str[1]));var_dump(ord('['));
如果是gbk的话,可以用下面的方式
$test = '新_建&文\件,夹a啊[?[ abd';$out = preg_split('/([a-zA-Z0-9]|[\x80-\xff].)+/', $test);$matches = array(); preg_match_all('/([a-zA-Z0-9]|[\x80-\xff].)/', $test, $matches);$res = implode($matches[1]);var_dump($res);$out = str_split(str_replace($matches[1], '', $test));var_dump($out);
#5 没效果...<?php$patten = "[^A-Za-z0-9\x{4e00}-\x{9fff}]";$str = "新_建&文\件,夹 abd";echo preg_replace("/".preg_quote($patten)."/",'',$str);#result:#新_建&文\件,夹 abd?>
OK,#6 的可以用了。谢谢各位支持。
#5 没效果...<?php$patten = "[^A-Za-z0-9\x{4e00}-\x{9fff}]";$str = "新_建&文\件,夹 abd";echo preg_replace("/".preg_quote($patten)."/",'',$str);#result:#新_建&文\件,夹 abd?>
使用utf8匹配规则,要在正则后面加u模式匹配,即应该是
echo preg_replace("/[^A-Za-z0-9\x{4e00}-\x{9fff}]/u",'',$str);
其实 \p{Han} 在某种意义上等同于 \x{4e00}-\x{9fff}

Laravel simplifies handling temporary session data using its intuitive flash methods. This is perfect for displaying brief messages, alerts, or notifications within your application. Data persists only for the subsequent request by default: $request-

The PHP Client URL (cURL) extension is a powerful tool for developers, enabling seamless interaction with remote servers and REST APIs. By leveraging libcurl, a well-respected multi-protocol file transfer library, PHP cURL facilitates efficient execution of various network protocols, including HTTP, HTTPS, and FTP. This extension offers granular control over HTTP requests, supports multiple concurrent operations, and provides built-in security features.

Laravel provides concise HTTP response simulation syntax, simplifying HTTP interaction testing. This approach significantly reduces code redundancy while making your test simulation more intuitive. The basic implementation provides a variety of response type shortcuts: use Illuminate\Support\Facades\Http; Http::fake([ 'google.com' => 'Hello World', 'github.com' => ['foo' => 'bar'], 'forge.laravel.com' =>

Do you want to provide real-time, instant solutions to your customers' most pressing problems? Live chat lets you have real-time conversations with customers and resolve their problems instantly. It allows you to provide faster service to your custom

Laravel's service container and service providers are fundamental to its architecture. This article explores service containers, details service provider creation, registration, and demonstrates practical usage with examples. We'll begin with an ove

PHP logging is essential for monitoring and debugging web applications, as well as capturing critical events, errors, and runtime behavior. It provides valuable insights into system performance, helps identify issues, and supports faster troubleshoot

Article discusses late static binding (LSB) in PHP, introduced in PHP 5.3, allowing runtime resolution of static method calls for more flexible inheritance.Main issue: LSB vs. traditional polymorphism; LSB's practical applications and potential perfo

The article discusses adding custom functionality to frameworks, focusing on understanding architecture, identifying extension points, and best practices for integration and debugging.


Hot AI Tools

Undresser.AI Undress
AI-powered app for creating realistic nude photos

AI Clothes Remover
Online AI tool for removing clothes from photos.

Undress AI Tool
Undress images for free

Clothoff.io
AI clothes remover

AI Hentai Generator
Generate AI Hentai for free.

Hot Article

Hot Tools

SublimeText3 Chinese version
Chinese version, very easy to use

SublimeText3 English version
Recommended: Win version, supports code prompts!

MantisBT
Mantis is an easy-to-deploy web-based defect tracking tool designed to aid in product defect tracking. It requires PHP, MySQL and a web server. Check out our demo and hosting services.

Dreamweaver CS6
Visual web development tools

WebStorm Mac version
Useful JavaScript development tools