正则过滤字符串
正则 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新建文件夹abd
$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);
echo preg_replace('/[^A-Za-z\p{Han}]+/u', '', "新_建&文\件,夹 abd");
<?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('['));
$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);