php字符串过滤与替换小结_php技巧
程序员文章站
2022-05-28 15:06:52
...
本文实例总结了php字符串过滤与替换的方法。分享给大家供大家参考。具体实现方法如下:
复制代码 代码如下:
class cls_string_filter{
//将 转化为
--囧,这有意思么?
static public function nl2br($string){
return nl2br($string);
}
//将
转化为
static public function br2nl($string){
$array = array('
','
');
return str_replace($array,"\n",$string);//字符串替换
}
//多个空格只保留一个
static public function merge_spaces($string){
return preg_replace("/\s(?=\s)/","\\1",$string);//(?=pattern)举例:abc(?=kk)能匹配abckk,但不能匹配abcdd
}
//多个
只保留一个
static public function merge_brs($string){
return preg_replace("/((
)+)/i","
",$string);//---"/"为什么也转义了
}
//过滤字符串中的html标签
static public function strip_tags($string){
return strip_tags($string);
}
//将字符串转换为小写--/--大写
static public function strtolower($string){
return strtolower($string);
}
static public function strtoupper($string){
return strtoupper($string);
}
//过滤字符串开头与结尾的特定字符
static public function trim($string,$char_list='\\\\s'){
$find = array('/[\^\-\]\\\]/S','/\\\{4}/S','/\//');
$replace = array('\\\\\\0','\\','\/');
$char = preg_replace($fine,$replace,$char_list);
$pattern = '^['.$chars.']*|['.$chars.']';
return preg_replace("/$pattern/sSD",'',$string);
}
//过滤字符串中
//将 转化为
--囧,这有意思么?
static public function nl2br($string){
return nl2br($string);
}
//将
转化为
static public function br2nl($string){
$array = array('
','
');
return str_replace($array,"\n",$string);//字符串替换
}
//多个空格只保留一个
static public function merge_spaces($string){
return preg_replace("/\s(?=\s)/","\\1",$string);//(?=pattern)举例:abc(?=kk)能匹配abckk,但不能匹配abcdd
}
//多个
只保留一个
static public function merge_brs($string){
return preg_replace("/((
)+)/i","
",$string);//---"/"为什么也转义了
}
//过滤字符串中的html标签
static public function strip_tags($string){
return strip_tags($string);
}
//将字符串转换为小写--/--大写
static public function strtolower($string){
return strtolower($string);
}
static public function strtoupper($string){
return strtoupper($string);
}
//过滤字符串开头与结尾的特定字符
static public function trim($string,$char_list='\\\\s'){
$find = array('/[\^\-\]\\\]/S','/\\\{4}/S','/\//');
$replace = array('\\\\\\0','\\','\/');
$char = preg_replace($fine,$replace,$char_list);
$pattern = '^['.$chars.']*|['.$chars.']';
return preg_replace("/$pattern/sSD",'',$string);
}
//过滤字符串中
希望本文所述对大家的php程序设计有所帮助。