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程序设计有所帮助。

相关文章

解析zend studio中直接导入svn中的项目的方法步骤

1.在zend-studio中的项目explorer中右键-》import->选择svn->project from svn-》next-》选择create new ...l...

php图片添加水印例子

图片添加水印我相信各位朋友都知道的,今天我们来看一段php的图片添加水印例子,希望文章能够帮助到各位朋友。 <?php /** * 图片添加水印...

php把数组值转换成键的方法

本文实例讲述了php把数组值转换成键的方法。分享给大家供大家参考。具体如下: function values2keys($arr, $value=1){ $new = array...

php中session垃圾回收机制

在PHP中,没有任何变量指向这个对象时,这个对象就成为垃圾。PHP会将其在内存中销毁;这是PHP的GC垃圾处理机制,防止内存溢出。 GC的工作就是扫描所有的Session信息,用当前时间...

PHP静态方法和静态属性及常量属性的区别与介绍

PHP中若使用static关键字来修饰属性、方法,称这些属性、方法为静态属性、静态方法。static关键字声明一个属性或方法是和类相关的,而不是和类的某个特定的实例相关,因此,这类属性或...