php字符串过滤与替换小结

yipeiwu_com6年前PHP代码库

本文实例总结了php字符串过滤与替换的方法。分享给大家供大家参考。具体实现方法如下:

复制代码 代码如下:
<?php
class cls_string_filter{
 //将\n转化为<br/>--囧,这有意思么?
 static public function nl2br($string){
  return nl2br($string);
 }
 //将<br/>转化为\n
 static public function br2nl($string){
  $array = array('<br>','<br/>');
  return str_replace($array,"\n",$string);//字符串替换
 }
 //多个空格只保留一个
 static public function merge_spaces($string){
  return preg_replace("/\s(?=\s)/","\\1",$string);//(?=pattern)举例:abc(?=kk)能匹配abckk,但不能匹配abcdd
 }
 //多个<br/>只保留一个
 static public function merge_brs($string){
  return preg_replace("/((<br\/?>)+)/i","<br>",$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);
 }
 //过滤字符串中<style>脚本
 static public function stric_style($string){
  $reg = "/<style[^>]*?>.*?<\/style>/is";
  return preg_replace($reg,'',$string);
 }
 //过滤字符串中html危险代码
 static public function strip_html_tags($string){
  $reg = "/(\/?)/(script|iframe|style|html|body|title|meta|\?|\%)([^>]*?>)/is";
  return preg_replace($reg,'',$string);
 }
}
?>

希望本文所述对大家的php程序设计有所帮助。

相关文章

php中利用post传递字符串重定向的实现代码

复制代码 代码如下: $ch = curl_init('http://domain-name.com/page.php');       curl_seto...

thinkphp中多表查询中防止数据重复的sql语句(必看)

下面先来看看例子: table id name 1 a 2 b 3 c 4 c 5 b 库结构大概这样,这只是一个简单的例子,实际情况会复杂得多。 select *, c...

PHP文件读写操作之文件写入代码

在PHP网站开发中,存储数据通常有两种方式,一种以文本文件方式存储,比如txt文件,一种是以数据库方式存储,比如Mysql,相对于数据库存储,文件存储并没有什么优势,但是文件读写操作在基...

基于php常用函数总结(数组,字符串,时间,文件操作)

数组:【重点1】implode(分隔,arr) 把数组值数据按指定字符连接起来例如:$arr=array('1','2','3','4');$str=implode('-',$arr);...

PHP开发不能违背的安全规则 过滤用户输入

作为最基本的防范你需要注意你的外部提交,做好第一面安全机制处理防火墙。 规则 1:绝不要信任外部数据或输入 关于Web应用程序安全性,必须认识到的第一件事是不应该信任外部数据。外部数据(...